我是 C++ 新手,在理解整个读取文件流的过程中遇到了一些麻烦。任何帮助将不胜感激...这就是我遇到问题的地方
我有一系列这样的结构;(不,我显然不允许使用字符串来存储这些东西,或者向量或我没有涉及的任何其他更高级的东西)......
struct Staff
{
char title[TITLESIZE];
char firstName[NAMESIZE];
char familyName[NAMESIZE];
char position[POSSIZE];
char room[TITLESIZE];
char email[POSSIZE];
};
然后我有一个这些结构的数组;
Staff record[MAXNOSTAFF];
数据包含在由制表符分隔的文本文件中。但是,某些字段可能包含空格。数据如下:
Dr Sherine ANTOUN Lecturer 4327 3.204 sherine_antoun@gmail.com
这是我在代码中写的...
//function prototypes
bool getRecord (ifstream& infile, Staff dataAr[], bool& fileFound);
int main()
{
Staff record[MAXNOSTAFF];
bool fileFound;
ifstream infile;
getRecord(infile, record, fileFound); //function call
if (fileFound==true)
{
cerr <<"Exiting Program"<<endl;
exit(1);
}
return 0;
}
//function definitions
bool getRecord (ifstream& infile, Staff dataAr[], bool& fileFound)
{
infile.open("phonebook.txt");
if (infile)
{
fileFound = true;
cout << "File " <<PHONEBOOK<< " opened successfully.\n\n";
}
else if (!infile)
{
fileFound = false;
cerr << "Error! File could not be opened. \n";
}
while (infile.good())
{
for (int lineIndex=0; lineIndex<MAXNOSTAFF; lineIndex++)
for (int titleIndex=0; titleIndex<TITLESIZE; titleIndex++)
{
cin.getline(dataAr[lineIndex].title[titleIndex], MAXNOSTAFF, '/t');
}
}
//check it works properly
for (int k=0;k<10; k++)
{
for (int m=0; m<11; m++)
{
cout << k <<". Title is : "<<dataAr[k].title[m]<<endl;
}
}
infile.close();
return fileFound;
}
任何帮助将不胜感激..谢谢