Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. i can't understand why this result is showed~

i can't understand why this result is showed~

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 6 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jung Seng Won
    wrote on last edited by
    #1

    if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

    firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

    _ C S CPalliniC S 5 Replies Last reply
    0
    • J Jung Seng Won

      if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

      firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      Your code appears to be correct. Did you cross check if that file contains three entries only?

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • J Jung Seng Won

        if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

        firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Check the documentation of fgets: If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned. The end of file is detected once you try to read after the last character of your file. So, in your case, you will still execute fputs even if the eof was detected. You should do something like this instead:

        char* pTemp = fgets(data, sizeof(data)-1, fp);
        while (pTemp)
        {
        fputs(data, stdout);
        pTemp = fgets(data, sizeof(data)-1, fp);
        }

        EDIT: oh and BTW, this kind of problem can be tracked quite easily by using your debugger. It is much faster than asking a question on a forum ;) .

        Cédric Moonen Software developer
        Charting control [v1.4] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • J Jung Seng Won

          if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

          firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #4

          Jung Seng Won wrote:

          while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); }

          Modify your while loop as below

          while ( !feof(fp) )
          {
          if( fgets(data, sizeof(data)-1, fp) == NULL)
          break;
          else
          fputs(data, stdout);
          }

          Regards, Sandip.

          1 Reply Last reply
          0
          • J Jung Seng Won

            if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

            firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Jung Seng Won wrote:

            why that result come?

            Because: Your file ends with an empty line and your program doesn't check fgets return value. Change

            Jung Seng Won wrote:

            fgets(data, sizeof(data)-1, fp); fputs(data, stdout);

            with

            if( fgets(data, sizeof(data)-1, fp) )
            fputs(data, stdout);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • J Jung Seng Won

              if i input three string, test1 test2 test3 and result is test1 test2 test3 test3 as you can see, test3 show two times. why that result come? void main() { char data[100]; int i; FILE *fp = fopen("D:\\Code\\test\\input.txt","rt"); int lens; int num; char ch; if(fp == NULL) { puts("Error"); exit(0); } while ( !feof(fp) ) { fgets(data, sizeof(data)-1, fp); fputs(data, stdout); } fclose(fp); }

              firstly, i am sorry, i am from korea. and i am not good in wriitting english so please understand~ Thank you everyone~

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Assuming you're using C++ forget about all that C-style file manipulation bollocks and do something like the following:

              // VanillaConsole.cpp : Defines the entry point for the console application.
              //
               
              #include "stdafx.h"
              #include <iostream>
              #include <fstream>
              #include <string>
              using namespace std;
               
              int main()
              {
              ifstream ifs("C:\\Data.txt");
              if (!ifs)
              {
              cerr << "Failed to open file!" << endl;
              return -1;
              }
               
              string line;
              while (getline(ifs, line))
              {
              cout << line << endl;
              }
               
              return 0;
              }

              Easier to understand and no limit on the length of a line.

              Steve

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups