我正在编辑一个 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;
}