2
이것은 C++ 입문서 5th edition 장의 17 장 (17.5.3)의 샘플 코드입니다.C++ 입문서 5th edition 장 17.5.3 fstream이 줄 바꿈을하지 않음
int main(void) {
fstream inOut("test.txt", fstream::ate | fstream::in | fstream::out);
if (!inOut)
{
cerr << "Unable to open file!" << endl;
return EXIT_FAILURE;
}
auto end_mark = inOut.tellg();
inOut.seekg(0, fstream::beg);
size_t cnt = 0;
string line;
while (inOut && inOut.tellg() != end_mark && getline(inOut, line))
{
cnt += line.size() + 1;
auto mark = inOut.tellg();
inOut.seekg(0, fstream::end);
inOut << cnt;
if (mark != end_mark)
{
inOut << " ";
}
inOut.seekg(mark);
}
inOut.seekg(0, fstream::end);
inOut << "\n";
return 0;
}
파일 test.txt
의 내용은 다음과 같습니다이 파일은 빈 줄로 끝나는 경우
abcd
efg
hi
j
<This is a blank new line>
점은, 그것은 예상됩니다으로이 코드가 작동합니다. 다른 단어에서,이로 파일을 변경합니다 :이 그
이abcd
efg
hi
j5 9 12
참고 : 파일이 빈 새로운 라인으로 끝나지 않는 경우, 그것은 것입니다
abcd
efg
hi
j
5 9 12 14
<This is a blank new line>
을하지만이 같은 출력 파일이 새 행으로 끝나지 않습니다. 제 질문은 빈 줄 바꿈은 어디에 있습니까? 애프터, 코드가 있습니다
inOut << "\n";
어떤 경우에도 새로운 빈 줄을 넣어야합니다. 어디가 잘못 됐니?
테스트 됨. 매력처럼 작동하십시오. :) 8 장 (8.1.2)을 리뷰하고 있습니다. – callofdutyops