从文件读取时,是在读取之前还是之后检查EOF条件



使用伪代码...

aFile = open("file.txt")
//x = aFile.readLine()  <- reading before checking end of file condition
while !aFile.endOfFile()
    //x = aFile.readLine() <- only read if you have not already read 
                                          from the file before the loop
    print(x)
    //x = aFile.readLine()  <- if reading before checking end of file, 
                            you will read again after printing the previous x value
end while
aFile.close()

我的问题是,您是否需要先从文件中读取,然后才能检查 endOfFile 条件是否为假,还是在从文件中读取之前检查条件?

要获取有关文件末尾的信息,您首先必须访问此文件(流(。然后这取决于您用来检查此条件的语言/功能。但是例如在 C 中你可以这样做:

FILE * pFile;
int c;
int n = 0;
pFile=fopen ("myfile.txt","r");
do {
  //First get character
  c = fgetc (pFile);
  //Then check for EOF(end of line)
} while (c != EOF);
fclose (pFile);

相关内容

最新更新