#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 << "please input a filename and it will be created.\n";
getline(cin, anotherfile);
fstream bfout(anotherfile.c_str(), ios::out | ios::app | ios::binary);
if (!bfout.is_open())
{
cout << "This file can't be open.\n";
exit(EXIT_FAILURE);
}
student stu;
int cnt = 0;
cout << "please input student's name(a blank line to quit):";
cin.getline(stu.name, 20);
while (stu.name[0] != '\0')
{
cnt++;
cout << "please input student's age:";
cin >> stu.age;
cout << "please input student's height:";
(cin >> stu.height).get();
bfout.write((char *)&stu, sizeof(stu));
cout << "please input student's name:(a blank line to quit)";
cin.getline(stu.name, 20);
}
bfout.close();
ifstream bfin;
bfin.clear();
bfin.open(anotherfile.c_str(), ios::in | ios::binary);
if (bfin.is_open())
{
cout << "The content of file Student.txt is:\n";
while (bfin.read((char *)&stu, sizeof stu))
{
cout.setf(ios_base::left, ios_base::adjustfield);
cout << " Name:" << setw(10) << stu.name << endl;
cout << " Age:" << setw(3) << stu.age << endl;
cout << "Height:" << setw(5)<<setprecision(4) << stu.height << endl;
}
bfin.close();
}
/*********************随机读取方式**********************************/
fstream bf(anotherfile.c_str(), ios::in | ios::out | ios::binary);
if (bf.is_open())
{
bf.seekg(0);//以字节为单位
cout << "The following is content of file " << anotherfile << ":\n";
while (bf.read((char *)&stu, sizeof stu))
{
cout.setf(ios_base::left, ios_base::adjustfield);
cout << " Name:" << setw(10) << stu.name << endl;
cout << " Age:" << setw(3) << stu.age << endl;
cout << "Height:" << setw(5) << setprecision(4) << stu.height << endl;
}
if (bf.eof())
bf.clear();
else
{
cerr << "Error in read file" << anotherfile << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << anotherfile << " can't be opened.\n";
exit(EXIT_FAILURE);
}
int position;//选择读取位置
cout << "please input this position you want to modify:";
(cin >> position).get();
if (position < 0 || position >= cnt)
{
cerr << "Invaild record number.\n";
exit(EXIT_FAILURE);
}
streampos pos = position*sizeof(struct student);
bf.seekg(pos);
bf.read((char *)&stu, sizeof stu);
cout << "Your selection is:\n";
cout.setf(ios_base::left, ios_base::adjustfield);
cout << " Name:" << setw(10) << stu.name << endl;
cout << " Age:" << setw(3) << stu.age << endl;
cout << "Height:" << setw(5) << setprecision(4) << stu.height << endl;
cout << "Next to modify content.\n";
cout << "please input student's name:(a blank line to quit)";
cin.getline(stu.name, 20);
cout << "please input student's age:";
cin >> stu.age;
cout << "please input student's height:";
(cin >> stu.height).get();
bf.seekg(pos);//seekp()相对于文件结束的字节位置
bf.write((char *)&stu, sizeof(stu));
if (bf.fail())
{
cerr << "Error to attempt write.\n";
exit(EXIT_FAILURE);
}
streampos nowpos = bf.tellg();//tellp()返回离文件结束位置字节数
cout << "Now, the file's pointer is located in " << nowpos << " bytes.\n";
bf.seekg(0);//以字节为单位,0代表第一个
cout << "The following is now content of file " << anotherfile << ":\n";
while (bf.read((char *)&stu, sizeof stu))
{
cout.setf(ios_base::left, ios_base::adjustfield);
cout << " Name:" << setw(10) << stu.name << endl;
cout << " Age:" << setw(3) << stu.age << endl;
cout << "Height:" << setw(5) << setprecision(4) << stu.height << endl;
}
if (bf.eof())
bf.clear();
else
{
cerr << "Error in read file" << anotherfile << endl;
exit(EXIT_FAILURE);
}
system("pause");
return 0;
}
程序运行结果如下
please input a filename you want to read:name.txt
The content of name.txt is:
Tom
Jack
Jerry
lucy
Chirsmas
please input another name for student:
China
Russia
The new content of name.txt is:
Tom
Jack
Jerry
lucy
ChirsmasChina
Russia
please input a filename and it will be created.
Student.txt
please input student's name(a blank line to quit):NameA
please input student's age:21
please input student's height:173.34
please input student's name:(a blank line to quit)NameB
please input student's age:23
please input student's height:175.2
please input student's name:(a blank line to quit)
The content of file Student.txt is:
Name:NameA
Age:21
Height:173.3
Name:NameB
Age:23
Height:175.2
The following is content of file Student.txt:
Name:NameA
Age:21
Height:173.3
Name:NameB
Age:23
Height:175.2
please input this position you want to modify:1
Your selection is:
Name:NameB
Age:23
Height:175.2
Next to modify content.
please input student's name:(a blank line to quit)NameC
please input student's age:19
please input student's height:172.5
Now, the file's pointer is located in 56 bytes.
The following is now content of file Student.txt:
Name:NameA
Age:21
Height:173.3
Name:NameC
Age:19
Height:172.5
请按任意键继续. . .