我想打开一个文件并从中读取一行。文件中只有一行,所以我真的不需要担心循环,尽管为了将来参考,知道如何读取多行会很好。
int main(int argc, const char* argv[]) {
// argv[1] holds the file name from the command prompt
int number = 0; // number must be positive!
// create input file stream and open file
ifstream ifs;
ifs.open(argv[1]);
if (ifs == NULL) {
// Unable to open file
exit(1);
} else {
// file opened
// read file and get number
...?
// done using file, close it
ifs.close();
}
}
我该怎么做?另外,就成功打开而言,我是否正确处理了文件打开?
谢谢。