一段简单的C++代码用于去除文本文件的一行
#include <cstdio> #include <fstream> #include <string> #include <iostream> using namespace std; int main() { string line; // open input file ifstream in("infile.txt"); if( !in.is_open()) { cout << "Input file failed to open\n"; return 1; } // now open temp output file ofstream out("outfile.txt"); // loop to read/write the file. Note that you need to add code here to check // if you want to write the line while( getline(in,line) ) { if(line != "I want to delete this line") out << line << "\n"; } in.close(); out.close(); // delete the original file remove("infile.txt"); // rename old to new rename("outfile.txt","infile.txt"); // all done! return 0; }