我用c ++为学生数据库创建了一个程序。该数据库有7个选项可供选择。例如输入学生详细信息,编辑记录等。通过case语句可以选择不同的选项。我的问题与情况6有关。在这种情况下,我必须将输入的文本显示到文本文件中,并能够对其进行读写。我可以写但不能读。我只张贴了案例6部分。 注意:我创建了一个班级来存储学生的信息,例如姓名,姓氏等,“ stud”是班级学生的对象。
class students
{
public:
char name[50];
char sname[45];
int idno;
char cours[40];
int age;
int modules;
void read_data();
void display();
}s;
void students::read_data()
{
cout<<"Enter name :";
cin>>name;
cout<<"Enter Surname : ";
cin>>sname;
cout<<"Enter course : ";
cin>>cours;
cout<<"Enter age :";
cin>>age;
cout<<"Enter modules:";
cin>>modules;
}
void students::display()
{
cout<<"-----------------------------------------------"<<endl;
cout<<"Student ID no : "<<idno<<endl;
cout<<"Student name is :"<<name<<endl;
cout<<"Student Surname is : "<<sname<<endl;
cout<<"Student course is :"<<cours<<endl;
cout<<"Student age is :"<<age<<endl;
cout<<"Student modules are:"<<modules<<endl;
cout<<"----------------------------------------------"<<endl;
}
int main()
{
char dow;
int arr=0;
do{
students stud[100];
cout<<"Press 1 Enter record \n";
cout<<"Press 2 Display record \n";
cout<<"\n\t Select option::";
int idcheck=0;
int sw;
cin>>sw;
switch(sw)
{
case 1:
cout<<"\n Enter the data of the student no "<<arr+1<<" is :\n";
cout<<"\t Enter the Roll No = ";
int id2;
int id;
cin>>id;
for(int j=0; j<arr; j++)
{
id2=id;
if(id2==stud[j].idno)
{
idcheck=1;
}
}
if(idcheck!=1){
stud[arr].idno=id;
stud[arr].read_data();
arr=arr+1;
}
else
{
cout<<"This Record is Already Entered \n";
}
break;
case:2
{
ifstream sfile;
sfile.open("StudentFile.txt", ios::in);
cout << "\n-----------------------------------------------" << endl;
for(int i = 0; i < 1; i++)
{
cout << setw(12) << "Rollno ||";
cout << setw(12) << " Name ||";
cout << setw(12) << " Surname || ";
cout << setw(12) << "Age ||";
cout << setw(12) << " Course ||";
cout << setw(12) << " Module ||";
for(int k = 0; k < arr; k++)
{
if(stud[k].idno != 'd')
{
cout << "\n";
cout << " ";
cout << setw(5) << stud[k].idno;
cout << setw(15) << stud[k].name;
cout << setw(14) << stud[k].sname;
cout << setw(14) << stud[k].age;
cout << setw(12) << stud[k].cours;
cout << setw(14) << stud[k].modules;
}
}
}
cout << "\n-------------------------------------------" << endl;
sfile.read((char*)&s, sizeof(s));
sfile.close();
}
答案 0 :(得分:0)
一个流包含两个“指针”,它们指向它正在读取和正在写入的位置。如果您正在写,则读指针也将前进。所以 您首先要向后移动读取指针,使您将使用seekg()读取的字节数:
cfile.seekg(-static_cast<long>(sizeof stud), cfile.cur);
#include <iostream>
#include <fstream>
struct Foo {
int i = 2;
double d = 3.13421;
float f = 3241.41f;
};
std::ostream& operator<<(std::ostream& s, const Foo& foo) {
return s << "i=" << foo.i << "; d=" << foo.d << "; f=" << foo.f;
}
int main() {
std::fstream file;
Foo foo;
file.open("foo.txt", file.in | file.out | file.trunc);
file << "some other text";
file.write(reinterpret_cast<char*>(&foo), sizeof foo);
std::cout << foo << std::endl;
foo.i = foo.d = foo.f = 0;
file.seekg(-static_cast<long>(sizeof foo), file.cur);
std::cout << foo << std::endl;
file.read(reinterpret_cast<char*>(&foo), sizeof foo);
std::cout << foo << std::endl;
}
产生以下输出:
i=2; d=3.13421; f=3241.41
i=0; d=0; f=0
i=2; d=3.13421; f=3241.41