1

我正在编辑一个 python 文件,我的程序遍历每一行,如果字符串“if”存在,它会向它附加注释。该程序确实可以工作,但它添加了 ^M,我无法再在 GitHub 上看到代码,因为它显示为原始文件。

在这里查看这篇文章https://unix.stackexchange.com/questions/32001/what-is-m-and-how-do-i-get-rid-of-it
我使用了推荐 dos2linux 然后运行我的程序, ^M 字符没有出现,但我仍然看不到 GitHub 上的代码。

这是有问题的代码

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;
}
4

1 回答 1

0

事实证明,使用 unix2dos 的解决方案奏效了。最终发生的事情是所有附加的评论都在文件中添加了另一个 .6MB,这反过来又使它超过了 1MB。这就是为什么即使代码可以在本地轻松阅读,您也无法在 GitHub 上查看它。

感谢那些评论的人,我感谢您的努力。

于 2020-09-03T18:13:54.937 回答