fstream 의 eof에 관해서 질문합니다.

wjdtlsckfu의 이미지

파일 인풋을 받는데 파일의 마지막 라인이 두번 읽힙니다. 왜그런건가요?

std::ifstream files(file);
while(!files.eof())
{
   if(cycle==0)
     files>>v1>>v2;
   else
   {
    files>>v1>>v2>>w;
    std::cout<<v1<<" "<<v2<<" "<<w<<std::endl;  
   }
   ++cycle;
}

input.txt는 이렇게 생겼습니다.

7 8
0 1 1
0 2 5
1 3 2
1 4 3
2 5 6
3 6 7
4 6 4
5 6 8

code를 간단하게 옮겨적다보니.. 이해하시기 어려울지도 모르겠네요. 일단 문제는 인풋을 저장해서 처리하는데에 있는게 아니라 인풋자체가 중복되서 들어 온다는것에 있음은 확실합니다.
왜 이러는걸까요 ? 도와주세요.

cinsk의 이미지

ios::eof()는 해당 stream에 eof state flag이 set되었다면 true를 리턴합니다. eof state flag을 set하는 것은 여러 reading member 함수에서 합니다.

마지막 줄을 "files>>v1>>v2>>w;"로 읽었을때, 원하는 데이터를 다 읽은 상태이기 때문에 eof state flag은 그대로 false입니다. 따라서 loop를 한 번 더 돌게 되며, 이 때 따로 error checking을 안했기 때문에, 이전 loop 돌때 기록되었던 v1, v2, w를 그대로 출력한 상태입니다.

1) 내용을 모르는 파일을 읽을 때에는 계속 에러 check를 해야 합니다. "files >> v1 >> v2 >> w"처럼, 세개의 데이터를 읽는데, 제대로 읽은 것인지 에러 checking을 하지 않았기 때문에, 이런 버그가 발생합니다.

2) 1에서 error check를 했다면, 궃이 eof()를 쓸 필요가 없습니다.

익명 사용자의 이미지

    std::ifstream files(file);
    while(1)
    {
        if(cycle==0)
            files>>v1>>v2;
        else
        {
            files>>v1>>v2>>w;
            if (files.eof()) break;
            std::cout<<v1<<" "<<v2<<" "<<w<<std::endl;  
        }
        ++cycle;
    }

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.