텍스트 파일의 개행 수를 읽으려고합니다. 그러나 제 카운터는 작동하지 않습니다. 문자열 비교 때문인가요?파일의 개행 수를 계산하십시오.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream myFile;
int temp_height = 0;
myFile.open("Levels.txt");
while (!myFile.eof())
{
getline(myFile,line);
if (line == "\n")
temp_height++;
}
cout<<"\n Newlines: "<<temp_height;
myFile.close();
}
빈 줄입니까? – BoBTFish
eof를 사용하는 것 외에도, 개행을 원할 경우 비어있는 행을 원하면 비어있는 행을 비교하기 위해 비교가 잘못되었습니다. – PlasmaHH
빈 줄 또는 모든 줄? 아, 빈 줄 ['std :: getline'] (http://en.cppreference.com/w/cpp/string/basic_string/getline)은 빈 문자열을 반환합니다. 분리 문자 (기본적으로 개행 문자)를 문자열에 추가하지 않습니다. –