2014-10-12 2 views
-3

왜 줄 수가 항상 0입니까? 그것은 10이어야하지만 출력은 항상 0입니다. 그 방법에 문제가 있습니까? while(!inputFile.eof())줄 수는 항상 0입니다.

int main() { 
    vector<double> doubleCoefficient; // vector to store unknown number of equations (double) 

    /* Read from file and assign values to vector */ 

    //File stream object 
    ifstream inputFile; 
    //Open the txt file 
    inputFile.open("test.txt"); 

    //search for the text file 
    if(!inputFile.is_open()) 
    { 
     cerr << "Error opening file \n"; 
     exit(EXIT_FAILURE); 
    } 
    else 
    { 
     cout << "File found and successfully opened. \n"; 
    } 
    double x; 

    while(!inputFile.eof()){ 

     inputFile >> x; 
     doubleCoefficient.push_back(x); 
    } 

    int count =0; 
    string line; 
    while (getline(inputFile, line)){ 
     count++; 
    } 
    cout << "Number of lines in text file:" << count << endl; 

    inputFile.close(); 
} 

답변

1

당신은 파일의 끝으로 이동, 그래서 그 후, 당신은 행을 읽어 두지. 당신은 선을 계산하기 전에

fseek (inputFile , 0 , SEEK_SET); 

을 시도 fseek()

사용하여 시작으로 다시 이동해야합니다.

+0

왜냐하면 당신은 file_의 끝에서부터 카운트하기 시작했기 때문에, 당신은 아무 것도 세지 않을 것입니다. – gkovacs90

+0

@ user3504305 pls 확인 업데이트 – gkovacs90

관련 문제