您的位置 首页 > 数码极客

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"; ("cad.dat"); if ( )) { cout << "Input file opening failed.\n"; exit(1); } ("c;); if ( )) { cout << "Output file opening failed.\n"; exit(1); } addPlusPlus(fin, fout); ( ); ( ); cout << "End of editing files.\n"; return 0; } void addPlusPlus(ifstream& inStream, ofstream& outStream) { char next; inS(next);//从文件读取一个字符给next while (! inS( )) { if (next == 'C') outStream << "C++";//输出字面常量给文件 else outStream << next;//输出变量next值给文件 inS(next); } }

以上程序运行后,会在当前目录下自动新建一文件c,其内容如下:

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-

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“c如何读入文件的一列数”边界阅读