• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

带你粗略了解C++流的读写文件

c++ 搞代码 4年前 (2022-01-06) 24次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要为大家总结了C++中输入输出流及文件流操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助

读写文本文件

C++的IO流:
IO:向设备输入数据和输出数据

设备有:
1)文件
2)控制台
3)特定的数据类型(stringstream)
C++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

C++的 IO类库为:

文件流:对文件进行读写操作
头文件:

ifstream 对文件输入(读文件)
ofstream 对文件输出(写文件)
fstream 对文件输入或输出

文件的打开方式:

模式标志 描述
ios::in 读方式打开文件
ios:out 写方式打开文件
ios::trunc 如果此文件已经存在, 就会打开文件之前把文件长度截断为0
ios::app 尾部最加方式(在尾部写入)
ios::ate 文件打开后, 定位到文件尾
ios::binary 二进制方式(默认是文本方式)

写文本文件

 #include  #include  #include  using namespace std; int main() { string name; int age; ofstream outfile;  //也可以使用fstream, 但是fstream的默认打开方式不截断文件长度 // ofstream的默认打开方式是,  截断式写入 ios::out |  ios::trunc // fstream的默认打开方式是,  截断式写入   ios::out // 建议指定打开方式 outfile.open("user.txt", ios::out | ios::trunc); while (1) { cout << "[ctrl+z退出]" << endl; cout <> name; if (cin.eof()) { //判断文件是否结束 break; } outfile << name << "\t"; cout <> age; outfile << age << endl;  //文本文件写入 } // 关闭打开的文件 outfile.close(); system("pause"); return 0; } 

写文本文件

 #include  #include  #include  using namespace std; int main() { string name; int age; ifstream infile; infile.open("user.txt"); while (1) { infile >> name; if (infile.eof()) { //判断文件是否结束 break; } cout << name <> age; cout << age << endl; } // 关闭打开的文件 infile.close(); system("pause"); return 0; } 

二进制读写文件

写二进制文件
使用文件流对象的write方法写入二进制数据.

注:若 ***outfile << age << end;***
写入文件会转换到文本方式写入
需要使用write(写)吧整形转换到char类型,进行写入

 #include  #include  #include  using namespace std; int main() { string name; int age; ofstream outfile; outfile.open("user.dat", ios::out | ios::trunc | ios::binary); while (1) { cout <> name; if (cin.eof()) { //判断文件是否结束 break; } outfile << name << "\t"; cout <> age; //outfile << age << endl;  //会自动转成文本方式写入 outfile.write((char*)&age, sizeof(age)); } // 关闭打开的文件 outfile.close(); system("pause"); return 0; } 

二进制读文件

需使用read(读)吧写入的内容读取出来并输出

 #include  #include  #include  using namespace std; int main() { string name; int age; ifstream infile; infile.open("user.dat", ios::in | ios::binary); while (1) { infile >> name; if (infile.eof()) { //判断文件是否结束 break; } cout << name <> age; //从文本文件中读取整数, 使用这个方式 infile.read((char*)&age, sizeof(age)); cout << age << endl;  //文本文件写入 } // 关闭打开的文件 infile.close(); system("pause"); return 0; } 

按指定格式读写文件

指定格式写文件:
使用

 #include  #include  #include  #include  using namespace std; int main() { string name; int age; ofstream outfile; outfile.open("user.txt", ios::out | ios::trunc); while (1) { cout << "[ctrl+z退出]" << endl; cout <> name; if (cin.eof()) { //判断文件是否结束 break; } cout <> age; stringstream s; s << "name:" << name << "\t\tage:" << age << endl; outfile << s.st<p style="color:transparent">来源gao!daima.com搞$代!码网</p>r(); } // 关闭打开的文件 outfile.close(); system("pause"); return 0; } 

指定格式读文件:
在C++指定格式读文件并没有优雅的解决方案
就用C语言的: sscanf

 #include  #include  #include  #include  #include  using namespace std; int main(void) { char name[32]; int age; string line; ifstream infile; infile.open

以上就是带你粗略了解C++流的读写文件的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:带你粗略了解C++流的读写文件

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址