【发布时间】:2015-06-05 01:41:50
【问题描述】:
所以这是我写的一个 void 函数,但是 << 显示为红色并且不会运行程序。这是我第一次遇到这个问题,我该怎么办?这是在 C++ 中完成的,我正在为我的编译器使用 Visual Studio。
void readData() //reading the function
{
ifstream inputfile;
inputfile.open("student.txt"); //open file for the students grades and names
for (int count = 0; count<20; count++)
{
inputfile **>>** st[count].studentFName >> st[count].studentLName; //reading each field
for (int i = 0; i<5; i++)
inputfile >> st[count].test_score[i]; //reading each score
}
inputfile.close(); //close the file
cout << " Reading of the file was completed!" <<endl;
}
**>>** 部分是我的错误发生的地方。它说它不是字符串的一部分?
【问题讨论】:
-
请在您的问题中发布
st[]的定义。 -
对不起,st是学生,所以学生数
-
这与您的问题无关,但最好不要将明确的
open和close调用与ifstream一起使用。相反,请使用 RAII:ifstream inputfile("student.txt")。 -
这将如何使文件打开或关闭?
-
流为您完成。
ifstream的构造函数打开文件,析构函数在 inputfile 超出范围时将其关闭..