c++githubdos2unix

C++ program adding ^M characters


I'm editing a python file and my program iterates through every line and if the string "if" exists, it appends a comment to it. The program does work but it adds ^M and I can no longer see the code on GitHub as it appears as a raw file.

Looking at this post here https://unix.stackexchange.com/questions/32001/what-is-m-and-how-do-i-get-rid-of-it
I used the commend dos2linux and then ran my program, and the ^M characters did not appear but I still cannot see the code on GitHub.

Here is the code in question

int main(){
    
    ifstream myfile ("file.py", ios::binary);
    ofstream newfile ("newfile.py", ios::binary);
    string line;
    string newline;
    
    string yep = "if";
    size_t check;

    while ( getline(myfile,line)){
        
        check = line.find("if");

        if ( check != string::npos){
            newline = line + "#If statement";
            newfile << newline << '\n';
        } else {
            newline = line;
            newfile << newline << '\n';
        }

    }


    myfile.close();
    newfile.close();
    return 0;
}

Solution

  • So as it turns out, the solution to use unix2dos worked. What ended up happening was all the comments being appended added another .6MB to the file which in turn made it over 1MB. That's why even though the code could be easily read locally, you could not view it on GitHub.

    Thank you to those who commented, I appreciate the effort.