File 只能从文本文件中读取一个名称
我有一个包含名称(char name[25])、年龄(int age)和gpa(float gpa)的结构(RECORD)。我正在尝试从此文本文件读取数据:File 只能从文本文件中读取一个名称,file,text,fstream,File,Text,Fstream,我有一个包含名称(char name[25])、年龄(int age)和gpa(float gpa)的结构(RECORD)。我正在尝试从此文本文件读取数据: Jacqueline Kennedy 33 3.5 克劳迪娅·约翰逊25 2.5 帕特·尼克松33 2.7 罗莎琳·卡特26 2.6 南希·里根193.5 芭芭拉·布什33 3.4 希拉里·克林顿25 2.5我认为这会有所帮助。首先,您需要读取数组中的整个文件,并将其转换为字符串数组,该数组的字符串长度为您所说的25个字符。然后,您只需遍
Jacqueline Kennedy 33 3.5
克劳迪娅·约翰逊25 2.5
帕特·尼克松33 2.7
罗莎琳·卡特26 2.6
南希·里根193.5
芭芭拉·布什33 3.4
希拉里·克林顿25 2.5
我认为这会有所帮助。首先,您需要读取数组中的整个文件,并将其转换为字符串数组,该数组的字符串长度为您所说的25个字符。然后,您只需遍历该数组即可显示它
if(f.is_open())
{
//file opened successfully so we are here
cout << "File Opened successfully!!!. Reading data from file into array" << endl;
//this loop run until end of file (eof) does not occ
while(!f.eof() && position < array_size)
{
f.get(array[position]); //reading one character from file to array
position++;
}
array[position-1] = '\0'; //placing character array terminating character
cout << "Displaying Array..." << endl << endl;
//this loop display all the charaters in array till \0
for(int i = 0; array[i] != '\0'; i++)
{
cout << array[i];
/*then you can divide the array in your desired length strings, which here is: Jacqueline Kennedy 33 3.5
Claudia Johnson 25 2.5
Pat Nixon 33 2.7
Rosalyn Carter 26 2.6
Nancy Reagan 19 3.5
Barbara Bush 33 3.4
Hillary Clinton 25 2.5*/
}
}
else //file could not be opened
{
cout << "File could not be opened." << endl;
}
如果(f.是开着的())
{
//文件已成功打开,所以我们在这里
库特