我正在尝试从文本文件中逐个字符地读取直到 EOF,将它们放入字符数组中,以便之后可以对其进行操作。用 g++ 编译没有错误,运行时,我被提示输入输入文件,但它只是挂起。
int main (int argc, char *argv[]) {
string filename;
ifstream infile;
char *cp, c[1024];
memset (c, 0, sizeof(c));
cp = c;
cout << "Enter file name: " << endl;
cin >> filename;
//open file
infile.open( filename.c_str() );
//if file can't open
if(!infile) {
cerr << "Error: file could not be opened" << endl;
exit(1);
}
while (!infile.eof()); {
infile.get(c, sizeof(infile));
// get character from file and store in array c[]
}
}//end main