C++对文件进行读写 *** 作范例代码

C++对文件进行读写 *** 作范例代码,第1张

概述C++对文件进行读写 *** 作范例代码

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#include <fstream>#include <iostream>using namespace std;  int main (){        char data[100];    // open a file in write mode.   ofstream outfile;   outfile.open("afile.dat");    cout << "Writing to the file" << endl;   cout << "Enter your name: ";   cin.getline(data,100);    // write inputted data into the file.   outfile << data << endl;    cout << "Enter your age: ";   cin >> data;   cin.ignore();       // again write inputted data into the file.   outfile << data << endl;    // close the opened file.   outfile.close();    // open a file in read mode.   ifstream infile;   infile.open("afile.dat");     cout << "Reading from the file" << endl;   infile >> data;    // write the data at the screen.   cout << data << endl;       // again read the data from the file and display it.   infile >> data;   cout << data << endl;    // close the opened file.   infile.close();    return 0;}

程序编译执行后输出如下结果
$./a.outWriting to the fileEnter your name: ZaraEnter your age: 9Reading from the fileZara9

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C++对文件进行读写 *** 作范例代码全部内容,希望文章能够帮你解决C++对文件进行读写 *** 作范例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1232155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存