问题描述
我正在编辑一个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;
}
解决方法
事实证明,使用unix2dos的解决方案有效。最终发生的事情是,所有附加的注释都在文件中添加了.6MB,从而使文件超过了1MB。这就是为什么即使可以在本地轻松读取代码,也无法在GitHub上查看它的原因。
谢谢那些发表评论的人,我很感激。