我基本上是在读取.txt
文件并存储值。
例如:
Student- Mark
Tennis
它将Mark
作为studentName
.
现在……如果只是
Student-
Tennis
然后它将正常工作并产生错误。
但是,如果文件看起来像这样
Student-(space)(nothing here)
Tennis
它将作为 存储Tennis
到内存中studentName
,如果事实上它不应该存储任何内容并产生错误。我使用'\n'
字符来确定字符之后是否有任何内容-
。这是我的代码...
istream& operator>> (istream& is, Student& student)
{
is.get(buffer,200,'-');
is.get(ch);
if(is.peek() == '\n')
{
cout << "Nothing there" << endl;
}
is >> ws;
is.getline(student.studentName, 75);
}
我认为这是因为is.peek()
正在识别空格,但是如果我尝试使用 删除空格is >> ws
,它会删除'\n'
字符并仍然存储Tennis
为studentName
.
如果有人可以帮助我解决这个问题,那真的意义重大。