[英]reading from a file c++ [program halts at reading put]
I'm having a problem reading in from a file, I feel disoriented and I cannot seem to find the error within my code(I'm sure it's a minor mistake because I've done this before) This a homework assignment. 我在读取文件时遇到问题,感到迷失方向,而且似乎无法在代码中找到错误(我确定这是一个小错误,因为我之前已经这样做过)这是一项家庭作业。 The assignment calls for the constructor to load in contents from a file. 该分配要求构造函数从文件中加载内容。 However, the program halts after reading in the first line. 但是,程序在第一行读取后停止。
tree::tree()
{
root = NULL;
load();
}
int tree::load()
{
ifstream inFile;
definition anEntry;
char title[TITLE], info[INFO];
inFile.open("CS_terms.txt");
if (inFile.is_open())
{
cin.get(title, TITLE, ':');
cin.ignore(TITLE, ':');
cin.get(info, INFO, '\n');
cin.ignore(INFO, '\n');
anEntry.createEntry(title, info);
insert(anEntry);
while (inFile.is_open() && !inFile.eof())
{
cin.get(title, TITLE, ':');
cin.ignore(TITLE, ':');
cin.get(info, INFO, '\n');
cin.ignore(INFO, '\n');
anEntry.createEntry(title, info);
insert(anEntry);
}
inFile.close();
return 1;
}
else
{
cout << "No File" << endl;
return 0;
}
}
You should replace this line: 您应该替换此行:
if (inFile)
by this 这样
if (inFile.is_open())
and this: 和这个:
while (inFile.is_open() && !inFile.eof())
by 通过
while (inFile.good())
尝试用inFile.get()
替换cin.get()
inFile.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.