今天小编就为大家分享一篇c++读取和写入TXT文件的整理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
#include "stdafx.h" #include //无论读写都要包含头文件 #include #include using namespace std; int main() { //ifstream从文件流向内存的ifstream表示文件输入流,意味着文件读操作 ifstream myfile("c://a.txt"); //ofstream从内存流向文件的ofstream表示文件输出流,意味着写文件操作 ofstream outfile("c://b.txt"); fstream fin1("file1.txt", ios::in); /*********************************************************************************************************/ //读文件的例子 //如果文件不存在(在visual stdio 2017环境下,文件存在了有.vcxproj文件的那个文件夹里面) if (!fin1) { //试了一下,用cout也可以,现在我还不明白为什么用cerr,为什么标准库要定义cerr这个对象 cerr << "读取失败" << endl; system("pause"); return -1; } else { cout << "读取成功" <= 65 && linestring[i] = 97 && linestring[i] <= 122) { //将字母字符存入磁盘文件 outfile.put(linestring[i]); cout << linestring[i] << ""; } cout << endl; outfile.close(); } } } /*********************************************************************************************************/ //写文件的例子 char ch; //以输入的方式打开文件 ifstream infile("f1.dat", ios::in); //如果文件不存在 if (!infile) { cerr << "open error!" << endl; exit(1); <em style="color:transparent">来源[email protected]搞@^&代*@码)网</em>} //定义输出流f3.dat文件 ofstream outfile("f3.dat"); if (!outfile) { cerr << "open error!" << endl; exit(1); } //当读取字符成功时 while (infile.get(ch)) { if (ch = 97) ch = ch - 32; outfile.put(ch); cout << ch; } cout << endl; infile.close(); outfile.close(); system("pause"); return 0; }
以上就是c++读取和写入TXT文件的整理方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!