我想编写一个小函数,它将文件作为输入,并通过以下更改写入输出文件:
\r\n) 作为 EndOfLine ,则应仅用 LF ( \n) 替换。\n),则应将其替换为 CRLF ( \r\n)有关这方面的更多信息,请参阅这篇文章。
这是我这样做的尝试:
bool convertFile(string location) {
ifstream input;
ofstream output;
input.open(location);
if(!input.is_open()){
cout << "Invalid location!" << endl;
return false;
}
int dot = location.find_last_of('.');
if(dot != string::npos) location.replace(dot, 1, "_new.");
output.open(location);
char c;
for(;;){
input.get(c);
if(!input.good()){
if(input.eof()) return true;
else return false;
}
if(c == '\r'){
input.get(c);
if(c == '\n') output << '\n'; // \r\n …Run Code Online (Sandbox Code Playgroud)