为什么我的程序不从文件中读取数据
why my program does not read the data from file
当我按“2”读取文件时,虽然我的语法是正确的,但无法读取文件中的数据。为什么我的程序不能读取数据?顺序访问文件和随机访问文件之间有什么区别,为什么首选随机访问文件?
void EnterRecord();
void ShowRecord();
using namespace std;
class student
{
int rollno;
string name;
int marks;
public:
void SetData();
void DisplayData();
void MainMenu();
};
void student:: MainMenu()
{
cout<<" "<<endl;
cout<<"press 1 to enter record"<<endl;
cout<<"press 2 to show record"<<endl;
}
void student::SetData()
{
cout<<"enter roll no of student"<<endl;
cin>>rollno;
cout<<"enter name of student"<<endl;
cin>>name;
cout<<"enter marks of student"<<endl;
cin>>marks;
}
void student::DisplayData()
{
cout<<rollno<<setw(10)<<setw(10)<<marks<<endl;
}
int main()
{
student s1;
int choice;
repeat:
s1.MainMenu();
cout<<"enter ur choice ::"<<endl;
cin>>choice;
switch(choice)
{
case 1:
EnterRecord();
break;
case 2:
ShowRecord();
break;
}
return 0;
}
void EnterRecord()
{
ofstream myfile;
myfile.open("student3.txt",ios::app);
int choice=1;
while(choice==1)
{
student s1;
s1.SetData();
myfile.write((char*)&s1,sizeof(student));
cout<<"press 1 to enter record again"<<endl;
cin>>choice;
if(choice!=1)
{
system("cls");
s1.MainMenu();
}
}
myfile.close();
}
void ShowRecord()
{
student s2;
ifstream myfile;
myfile.open("student3.txt");
myfile.seekg(0);
while(myfile.eof())
{
myfile.read((char*)&s2,sizeof(student));
}
s2.DisplayData();
}
应该是:
switch(choice)
{
case '1':
EnterRecord();
break;
case '2':
ShowRecord();
break;
}
最大的问题是,您将 student
的实例视为单个普通旧数据 (POD) 块,而实际上它不是。它包含 std::string
的实例,其中包含您不知道的内部数据。第一个也是最明显的是它包含一个指向字符串数据的指针。当你写出来时,你保存的是指针值而不是实际的字符串。当您将其重新加载时,它会读取 old 指针值,该值仅在您是真正幸运的妖精时才有效。
您的函数 EnterRecord
和 ShowRecord
将需要更新,以便它们序列化 student
的每个成员,而不是将其作为单个普通旧数据块写出。像下面这样的内容应该可以帮助您入门。
正在保存数据:
myfile << st.rollno;
myfile << st.name;
myfile << st.marks;
正在加载数据
myfile >> st.rollno;
myfile >> st.name;
myfile >> st.marks;
由于您没有提供有关文件布局的任何信息,因此具体如何布局由您决定。传统上你会重载 operator>>
和 operator<<
但因为这看起来像家庭作业,所以可能有点多。
您的代码还有其他几个问题...
您的 case 表达式不正确。您使用的 values 1 和 2 与 1 和 2 键的 ASCII 值不同.您应该使用“1”和“2”而不是简单的 1 和 2。
您还应该开始检查您调用的函数返回的错误,例如 open
.
在您的 while 循环中检查 eof
肯定不会起作用。查看问答@Why is iostream::eof inside a loop condition considered wrong?
当我按“2”读取文件时,虽然我的语法是正确的,但无法读取文件中的数据。为什么我的程序不能读取数据?顺序访问文件和随机访问文件之间有什么区别,为什么首选随机访问文件?
void EnterRecord();
void ShowRecord();
using namespace std;
class student
{
int rollno;
string name;
int marks;
public:
void SetData();
void DisplayData();
void MainMenu();
};
void student:: MainMenu()
{
cout<<" "<<endl;
cout<<"press 1 to enter record"<<endl;
cout<<"press 2 to show record"<<endl;
}
void student::SetData()
{
cout<<"enter roll no of student"<<endl;
cin>>rollno;
cout<<"enter name of student"<<endl;
cin>>name;
cout<<"enter marks of student"<<endl;
cin>>marks;
}
void student::DisplayData()
{
cout<<rollno<<setw(10)<<setw(10)<<marks<<endl;
}
int main()
{
student s1;
int choice;
repeat:
s1.MainMenu();
cout<<"enter ur choice ::"<<endl;
cin>>choice;
switch(choice)
{
case 1:
EnterRecord();
break;
case 2:
ShowRecord();
break;
}
return 0;
}
void EnterRecord()
{
ofstream myfile;
myfile.open("student3.txt",ios::app);
int choice=1;
while(choice==1)
{
student s1;
s1.SetData();
myfile.write((char*)&s1,sizeof(student));
cout<<"press 1 to enter record again"<<endl;
cin>>choice;
if(choice!=1)
{
system("cls");
s1.MainMenu();
}
}
myfile.close();
}
void ShowRecord()
{
student s2;
ifstream myfile;
myfile.open("student3.txt");
myfile.seekg(0);
while(myfile.eof())
{
myfile.read((char*)&s2,sizeof(student));
}
s2.DisplayData();
}
应该是:
switch(choice)
{
case '1':
EnterRecord();
break;
case '2':
ShowRecord();
break;
}
最大的问题是,您将 student
的实例视为单个普通旧数据 (POD) 块,而实际上它不是。它包含 std::string
的实例,其中包含您不知道的内部数据。第一个也是最明显的是它包含一个指向字符串数据的指针。当你写出来时,你保存的是指针值而不是实际的字符串。当您将其重新加载时,它会读取 old 指针值,该值仅在您是真正幸运的妖精时才有效。
您的函数 EnterRecord
和 ShowRecord
将需要更新,以便它们序列化 student
的每个成员,而不是将其作为单个普通旧数据块写出。像下面这样的内容应该可以帮助您入门。
正在保存数据:
myfile << st.rollno;
myfile << st.name;
myfile << st.marks;
正在加载数据
myfile >> st.rollno;
myfile >> st.name;
myfile >> st.marks;
由于您没有提供有关文件布局的任何信息,因此具体如何布局由您决定。传统上你会重载 operator>>
和 operator<<
但因为这看起来像家庭作业,所以可能有点多。
您的代码还有其他几个问题...
您的 case 表达式不正确。您使用的 values 1 和 2 与 1 和 2 键的 ASCII 值不同.您应该使用“1”和“2”而不是简单的 1 和 2。
您还应该开始检查您调用的函数返回的错误,例如
open
.在您的 while 循环中检查
eof
肯定不会起作用。查看问答@Why is iostream::eof inside a loop condition considered wrong?