#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
#include"windows.h"
struct student
{
char name[20];
int age;
float height;
};
using namespace std;
int main()
{
/*********************文本读取方式*********************************/
string filename;
cout << "please input a filename you want to read:";
getline(cin, filename);
ifstream fin;
fin.open(filename.c_str(), ios_base::in);
if (fin.is_open())
{
cout << "The content of name.txt is:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << endl;
fin.close();
}
ofstream fout(filename.c_str(), ios::out | ios::app);
if (!fout.is_open())
{
cout << "File " << filename << " can't open.\n";
exit(EXIT_FAILURE);
}
cout << "please input another name for student:\n";
string newname;
while (getline(cin, newname) && newname.size()>0)
{
fout << newname << endl;
}
fout.close();
fin.clear();
fin.open(filename.c_str());
if (fin.is_open())
{
cout << "The new content of name.txt is:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << endl;
fin.close();
}
/*********************二进制读取方式*******************************/
//这种适合于结构或者类数据读取方式,将整个对象内存量作为一个单位
string anotherfile;
cout << "pleas
C++文件输入输出流
最新推荐文章于 2024-08-08 22:16:16 发布
本文详细介绍了C++中如何进行文件的输入输出操作,包括打开文件、读取文件内容、写入文件以及关闭文件等关键步骤。通过实例代码演示了如何实现文件的读写操作,帮助读者掌握这一基础但重要的编程技能。
摘要由CSDN通过智能技术生成