2017-11-15 5 views
1

을 읽은 후 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 조건이 보유하고 확인할 수 있습니다 전에 파일에서 읽을 필요가있다 false 또는 파일에서 읽기 전에 조건을 확인하겠습니까?

답변

0

파일 끝에 대한 정보를 얻으려면 먼저이 파일 (스트림)에 액세스해야합니다. 그런 다음 언어/기능에 따라 다릅니다.이 상태를 확인하는 데 사용하고 있습니다. 예를 들어 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); 
관련 문제