fstream ::写和fstream ::阅读更改阅读和写作指针
我正在学习如何在文件上写记录并阅读它。
我创建了一个名为student
的类,该类具有sterstudent
,showstudent
,printinSideFile
(学生信息),, ......
当我尝试将学生信息写入文件中时,它可以工作。
当我尝试阅读文件中的所有学生信息时,它可以正常工作
当我尝试做两者时,我认为file.flush()
的问题发生了一些意外的事情(什么都没有出现)
,因此,当我删除它时,输出不可读取文本
我可以关闭文件并再次打开文件,但我认为这很愚蠢
代码为:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class student {
private:
char id[5], name[20], age[5], address[50], gender[5];
public:
void enterStudent() {
cout << "Enter student id : "; cin >> id;
cout << "\nEnter student name : "; cin >> name;
cout << "\nEnter student age : "; cin >> age;
cout << "\nEnter student address : "; cin >> address;
cout << "\nEnter student gender : "; cin >> gender;
}
void showStudent() {
cout << "#########student data##########\n";
cout << "student id : "<< id;
cout << "\nstudent name : " << name;
cout << "\nstudent age : " << age;
cout << "\nstudent address : " << address;
cout << "\nstudent gender : " << gender<<endl;
}
void printInsideFile(fstream &file) {
file.write(id,sizeof(id));
file.write(name, sizeof(name));
file.write(age, sizeof(age));
file.write(address, sizeof(address));
file.write(gender, sizeof(gender));
file.flush();
}
bool readFromFile(fstream &file) {
if (file.eof())
return 0;
file.read(id, sizeof(id));
file.read(name, sizeof(name));
file.read(age, sizeof(age));
file.read(address, sizeof(address));
file.read(gender, sizeof(gender));
if (file.eof())
return 0;
return 1;
}
void showAllFromFile(fstream &file) {
while (this->readFromFile(file))
this->showStudent();
}
};
int main() {
student s;
fstream file;
file.open("a.txt", ios::in | ios::out | ios::app);
if (!file)
goto k270;
s.enterStudent();
s.printInsideFile(file);
//when i read one student , it works okay
//s.readFromFile(file);
//s.showStudent();
//when i try to read multiple students , it doesn't work at all
s.showAllFromFile(file);
file.close();
k270:
system("pause");
return 0;
}
I am learning about how to write records on file and read it .
I created a class called student
, the class has function like enterStudent
, showStudent
, printInsideFile
(student info) ,...... .
when I try to write student info into the file , it works .
when I try to read all student info from the file , it works
when I try to do both , something unexpected happens (nothing show up)
i thought that the problem with file.flush()
, so when i deleted it the output was unreadable text
i could close the file and open it again but i think that is silly
the code is:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class student {
private:
char id[5], name[20], age[5], address[50], gender[5];
public:
void enterStudent() {
cout << "Enter student id : "; cin >> id;
cout << "\nEnter student name : "; cin >> name;
cout << "\nEnter student age : "; cin >> age;
cout << "\nEnter student address : "; cin >> address;
cout << "\nEnter student gender : "; cin >> gender;
}
void showStudent() {
cout << "#########student data##########\n";
cout << "student id : "<< id;
cout << "\nstudent name : " << name;
cout << "\nstudent age : " << age;
cout << "\nstudent address : " << address;
cout << "\nstudent gender : " << gender<<endl;
}
void printInsideFile(fstream &file) {
file.write(id,sizeof(id));
file.write(name, sizeof(name));
file.write(age, sizeof(age));
file.write(address, sizeof(address));
file.write(gender, sizeof(gender));
file.flush();
}
bool readFromFile(fstream &file) {
if (file.eof())
return 0;
file.read(id, sizeof(id));
file.read(name, sizeof(name));
file.read(age, sizeof(age));
file.read(address, sizeof(address));
file.read(gender, sizeof(gender));
if (file.eof())
return 0;
return 1;
}
void showAllFromFile(fstream &file) {
while (this->readFromFile(file))
this->showStudent();
}
};
int main() {
student s;
fstream file;
file.open("a.txt", ios::in | ios::out | ios::app);
if (!file)
goto k270;
s.enterStudent();
s.printInsideFile(file);
//when i read one student , it works okay
//s.readFromFile(file);
//s.showStudent();
//when i try to read multiple students , it doesn't work at all
s.showAllFromFile(file);
file.close();
k270:
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到我对 std :: ios :: app ,我很想知道它实际上是如何工作的。
我做了以下 mcve on Coliru 我相信(我相信)在预期的工作原理OP:
测试会话:
注意:
i完全确定
file.seekg(0)
,在读取所有
之前都需要将读取文件头倒入文件的开始。我不确定
阅读最后的书面行
…我发现
file.seekg(pos0);
是必要的。显然,上一篇文章似乎触及了(当然)和读取头。
我很难使用 std ::::::: istream :: tellg()。
它在第一个文件输出之后返回了预期值,但不以前。
我的最后一个度假胜地是
file.seekg(0,std :: fstream :: end);
在写入任何内容之前将读取头移至文件末端。对于示例中完全缺乏任何错误检查,我深表歉意。
当然,这必须在严重的应用中添加。
我遗漏了它以使示例代码尽可能最小。
此外,我没有考虑在操作示例代码中处理问题的错误。
Considering that I have least experience with std::ios::app, I became curious to find out how it actually works.
I made the following MCVE on coliru which (I believe) is working as expected by the OP:
Test Session:
Notes:
I was fully sure about the
file.seekg(0)
which is needed beforeread all
to rewind the read file head to the begin of file.I was not sure about the
read the last written line
…I found that the
file.seekg(pos0);
is necessary.Obviously, the previous write seems to touch the write head (of course) as well as the read head.
I had some trouble to retrieve the proper read head position using std::istream::tellg().
It returned the expected value after the first file output but not before.
My last resort was a
file.seekg(0, std::fstream::end);
to move the read head to end of file before anything is written.I apologize for the total lack of any error checking in the example.
Of course, this has to be added in a serious application.
I left it out to keep the sample code as minimal as possible.
Additionally, I didn't consider the error handling a problem in OPs sample code.
问题不是 ios ::应用
,似乎functions
file.write()
和file.read()
更改读取指针和编写指针。即使您使用
file&lt;&lt;“ text”;
或file&gt;&gt; gt; gt; and of chars;
两个指针都在更改。我搜索了它,但没有找到解释,但是我找到了 istream :: read ,它们是先进的
istream.tcc.html#_znst13basic_istream4Readept_l“ rel =“ nofollow 文件的结尾,它不会打印出什么
The problem was not the ios::app
It seems that functions
file.write()
andfile.read()
change either reading pointer and writing pointer .Even if you use
file<<"text";
orfile>>array of chars;
both of pointers are changing .I searched about it but didn't find explanation but i find the code of ostream::write and istream::read ,they were advanced so if anybody check the link and tell us why
note that ,when the reading pointer points at the end of the file , it will print out nothing