problem with fgetc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pavan734@gmail.com

    #1

    problem with fgetc

    Hi I have got a file "file_data. in" that contains the following data:
    abcdef
    I have a code that reads character by character & display it. But to
    my surprise it is printing one character more at the end. I dont want
    the last character to be printed. Here is the code I have written:
    //code
    FILE* input_file ;
    input_file = fopen("file_dat a.in", "r");
    char file_data;
    cout << "Printing input data\n" ;
    do
    {
    file_data = fgetc(input_fil e);
    if(file_data == EOF)
    {
    cout << "break\n" ;
    break;
    }
    cout << "File data = " << file_data << endl ;
    }while(file_dat a !=EOF);
    The output I got is :

    Printing input data
    File data = a
    File data = b
    File data = c
    File data = d
    File data = e
    File data = f
    File data =

    break

  • Ian Collins

    #2
    Re: problem with fgetc

    pavan734@gmail. com wrote:
    Hi I have got a file "file_data. in" that contains the following data:
    abcdef
    I have a code that reads character by character & display it. But to
    my surprise it is printing one character more at the end. I dont want
    the last character to be printed. Here is the code I have written:
    Is there a '\n' at the end of your input?
    //code
    FILE* input_file ;
    input_file = fopen("file_dat a.in", "r");
    Any good reason for using C style I/O?

    --
    Ian Collins.

    Comment

    • pavan734@gmail.com

      #3
      Re: problem with fgetc

      On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      pavan...@gmail. com wrote:
      Hi I have got a file "file_data. in" that contains the following data:
      abcdef
      I have a code that reads character by character & display it. But to
      my surprise it is printing one character more at the end. I dont want
      the last character to be printed. Here is the code I have written:
      >
      Is there a '\n' at the end of your input?
      >
      //code
      FILE* input_file ;
      input_file = fopen("file_dat a.in", "r");
      >
      Any good reason for using C style I/O?
      >
      --
      Ian Collins.
      No, there is no \n. Please try running the code. I dont have option to
      attach the file. Otherwise I would have done it.

      Comment

      • Ian Collins

        #4
        Re: problem with fgetc

        pavan734@gmail. com wrote:
        On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >
        >>pavan...@gmai l.com wrote:
        >>
        >>>Hi I have got a file "file_data. in" that contains the following data:
        >>>abcdef
        >>>I have a code that reads character by character & display it. But to
        >>>my surprise it is printing one character more at the end. I dont want
        >>>the last character to be printed. Here is the code I have written:
        >>
        >>Is there a '\n' at the end of your input?
        >>
        >>
        >>>//code
        >> FILE* input_file ;
        >> input_file = fopen("file_dat a.in", "r");
        >>
        >>Any good reason for using C style I/O?
        >>
        *Please* don't quote signatures!
        >
        No, there is no \n. Please try running the code. I dont have option to
        attach the file. Otherwise I would have done it.
        >
        I bet there is, change the output line to

        cout << "File data = " << (int)file_data << endl;

        and see what you get.

        --
        Ian Collins.

        Comment

        • Kai-Uwe Bux

          #5
          Re: problem with fgetc

          pavan734@gmail. com wrote:
          On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          >pavan...@gmail .com wrote:
          Hi I have got a file "file_data. in" that contains the following data:
          abcdef
          I have a code that reads character by character & display it. But to
          my surprise it is printing one character more at the end. I dont want
          the last character to be printed. Here is the code I have written:
          >>
          >Is there a '\n' at the end of your input?
          >>
          //code
          FILE* input_file ;
          input_file = fopen("file_dat a.in", "r");
          >>
          >Any good reason for using C style I/O?
          >>
          >--
          >Ian Collins.
          >
          No, there is no \n.
          Try opening the file in binary mode. It is conceivable that your OS is
          adding \n when it thinks you are finished reading the last line of a text
          file.
          Please try running the code. I dont have option to
          attach the file. Otherwise I would have done it.

          Best

          Kai-Uwe Bux

          Comment

          • pavan734@gmail.com

            #6
            Re: problem with fgetc

            On Apr 3, 1:14 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            pavan...@gmail. com wrote:
            On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >
            >pavan...@gmail .com wrote:
            >
            >>Hi I have got a file "file_data. in" that contains the following data:
            >>abcdef
            >>I have a code that reads character by character & display it. But to
            >>my surprise it is printing one character more at the end. I dont want
            >>the last character to be printed. Here is the code I have written:
            >
            >Is there a '\n' at the end of your input?
            >
            >>//code
            > FILE* input_file ;
            > input_file = fopen("file_dat a.in", "r");
            >
            >Any good reason for using C style I/O?
            >
            *Please* don't quote signatures!
            >
            No, there is no \n. Please try running the code. I dont have option to
            attach the file. Otherwise I would have done it.
            >
            I bet there is, change the output line to
            >
            cout << "File data = " << (int)file_data << endl;
            >
            and see what you get.
            >
            --
            Ian Collins.- Hide quoted text -
            >
            - Show quoted text -
            Yes you are true. It is also reading \n, but I didnt do "Enter" . Tell
            me how to avoid this

            Comment

            • pavan734@gmail.com

              #7
              Re: problem with fgetc

              On Apr 3, 1:13 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
              pavan...@gmail. com wrote:
              On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
              pavan...@gmail. com wrote:
              Hi I have got a file "file_data. in" that contains the following data:
              abcdef
              I have a code that reads character by character & display it. But to
              my surprise it is printing one character more at the end. I dont want
              the last character to be printed. Here is the code I have written:
              >
              Is there a '\n' at the end of your input?
              >
              //code
              FILE* input_file ;
              input_file = fopen("file_dat a.in", "r");
              >
              Any good reason for using C style I/O?
              >
              --
              Ian Collins.
              >
              No, there is no \n.
              >
              Try opening the file in binary mode. It is conceivable that your OS is
              adding \n when it thinks you are finished reading the last line of a text
              file.
              >
              Please try running the code. I dont have option to
              attach the file. Otherwise I would have done it.
              >
              Best
              >
              Kai-Uwe Bux- Hide quoted text -
              >
              - Show quoted text -
              Even if I open in binary mode, Iam getting the same problem

              Comment

              • P.J. Plauger

                #8
                Re: problem with fgetc

                <pavan734@gmail .comwrote in message
                news:1175582606 .512823.70080@d 57g2000hsg.goog legroups.com...
                Hi I have got a file "file_data. in" that contains the following data:
                abcdef
                I have a code that reads character by character & display it. But to
                my surprise it is printing one character more at the end. I dont want
                the last character to be printed. Here is the code I have written:
                //code
                FILE* input_file ;
                input_file = fopen("file_dat a.in", "r");
                char file_data;
                Here's one pending problem:

                EOF is an int value, which you're later storing in this
                char object. If char has a signed representation, you'll
                luck out (at the cost of mistaking 0xff for EOF).
                cout << "Printing input data\n" ;
                do
                {
                file_data = fgetc(input_fil e);
                if(file_data == EOF)
                {
                cout << "break\n" ;
                break;
                }
                cout << "File data = " << file_data << endl ;
                }while(file_dat a !=EOF);
                The output I got is :
                >
                Printing input data
                File data = a
                File data = b
                File data = c
                File data = d
                File data = e
                File data = f
                File data =
                >
                break
                A C text file is supposed to end on a newline, and it looks
                like that's what you're reading and printing out. If the
                file *doesn't* containe a newline, the C runtime may well
                be supplying one, or doing something else funny, right at
                the end of the file.

                HTH,

                P.J. Plauger
                Dinkumware, Ltd.



                Comment

                • James Kanze

                  #9
                  Re: problem with fgetc

                  On Apr 3, 10:21 am, pavan...@gmail. com wrote:
                  On Apr 3, 1:14 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                  pavan...@gmail. com wrote:
                  On Apr 3, 12:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                  >>pavan...@gmai l.com wrote:
                  >>>Hi I have got a file "file_data. in" that contains the following data:
                  >>>abcdef
                  >>>I have a code that reads character by character & display it. But to
                  >>>my surprise it is printing one character more at the end. I dont want
                  >>>the last character to be printed. Here is the code I have written:
                  >>Is there a '\n' at the end of your input?
                  >>>//code
                  >> FILE* input_file ;
                  >> input_file = fopen("file_dat a.in", "r");
                  >>Any good reason for using C style I/O?
                  No, there is no \n. Please try running the code. I dont have option to
                  attach the file. Otherwise I would have done it.
                  I bet there is, change the output line to
                  cout << "File data = " << (int)file_data << endl;
                  and see what you get.
                  Yes you are true. It is also reading \n, but I didnt do "Enter" . Tell
                  me how to avoid this
                  Well, the new line is in the file, so the problem is with
                  whatever program generates the file. Good text editors
                  generally have an option to force a newline at the end of all
                  files, and some text editors never do otherwise, at least when
                  editing text. A number of programs have, historically, had
                  problems when text files didn't end with a new line. Note that
                  in text mode, if the file doesn't end with a new line, the
                  behavior may vary---you may see a new line anyway, you may see
                  exactly what is there, or you may not see anything behind the
                  last new line.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  Working...