2017-11-05 5 views
-3

나는 파일 처리를 사용하여 시작하지 않는 줄의 수를 알아내는 간단한 프로그램을 만들고있다. 루프의 값보다 0에서 1까지 명확하게 실행해야하는 경우 루프가 4 회만 실행되는 이유를 이해할 수 없습니다. 여기에 어떤 문제가 있습니까?루프를위한 파일 처리

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
    const int s = 80; 
    char str[s]; 
    int lines = 0, a = 0, alines = 0; 
    ofstream fout("file6.txt", ios::out); 
    cout<<"Enter the number of lines you want to enter: "; 
    cin>>lines; 
    for(int i = 0; i < lines; i++) 
    { 
      cin.getline(str, 80); 
      fout<<str<<endl; 
    } 
    return 0; 
} 
+0

"라인의 수는 사용하여 파일 처리로 시작하지."는로 뭐? 거기에 중요한 정보를 떨어 뜨린 것처럼 보입니다. – user4581301

+2

서식이 지정된 입력은 줄 끝 문자를 읽지 않습니다. 첫 번째 getline은 다음 getline이 실제로 입력 할 때까지 대기합니다. 그게 네 문제라고 생각해? –

+0

https://stackoverflow.com/questions/1744665/need-help-with-getline –

답변

0

난 당신이 같은 파일에서 라인을 읽고 파일, 표준의 getline을 읽을 ifstream을 사용한다 생각 :

#include <iostream> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cmath> 
#include <sstream> 
#include <fstream> 


int main() 
{ 
    std::ifstream is("file6.txt"); 
    std::string line; 

    int linesToRead = 0; 

    std::cin>>linesToRead; 

    while(std::getline(is, line)) 
    { 
     if(linesToRead <= 0) 
      break; 
     std::cout<<line<<std::endl; 
     --linesToRead; 
    } 
    return 0; 
} 
+0

당신의 프로그램은 OP가 가지고 있다고 가정하는 동일한 문제를 가질 것이다. 이것은 형식화 된 입력 뒤에 나머지 공백과 EOL 문자를 다루는 것이다. –