c語言如何查找目錄中的文件?有文件cad.dat,其内容如下:C is one of the world's most modern,我來為大家科普一下關于c語言如何查找目錄中的文件?下面希望有你要的答案,我們一起來看看吧!
有文件cad.dat,其内容如下:
C is one of the world's most modern
programming languages. There is no
language as versatile as C, and C
is fun to use.
現在的任務是,讀取以上文件的内容,并将其中的字符“C“替換為“C “并寫入文件。
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; void addPlusPlus(ifstream& inStream, ofstream& outStream); int main( ) { ifstream fin; ofstream fout; cout << "Begin editing files.\n"; fin.open("cad.dat"); if (fin.fail( )) { cout << "Input file opening failed.\n"; exit(1); } fout.open("cplusad.dat"); if (fout.fail( )) { cout << "Output file opening failed.\n"; exit(1); } addPlusPlus(fin, fout); fin.close( ); fout.close( ); cout << "End of editing files.\n"; return 0; } void addPlusPlus(ifstream& inStream, ofstream& outStream) { char next; inStream.get(next);//從文件讀取一個字符給next while (! inStream.eof( )) { if (next == 'C') outStream << "C ";//輸出字面常量給文件 else outStream << next;//輸出變量next值給文件 inStream.get(next); } }
以上程序運行後,會在當前目錄下自動新建一文件cplusad.dat,其内容如下:
C is one of the world's most modern programming languages. There is no language as versatile as C , and C is fun to use.
-End-
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!