extra character when writing file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    #1

    extra character when writing file

    Hi There,

    I'm trying to read a file character by character. When I write the
    file out, there is one extra character which shows on the screen as a
    solid circle with a small question mark in the middle.

    Here is what I have:

    infile = fopen("encrypte d.txt", "r");
    outfile = fopen("plain.tx t", "w");

    while(!feof(inf ile)) {
    ch = fgetc(infile);
    fputc(ch, outfile);
    }

    fclose(infile);
    fclose(outfile) ;

    Can someone please tell me why this character is getting added to the
    outfile?
  • Richard Heathfield

    #2
    Re: extra character when writing file

    Jim said:
    Hi There,
    >
    I'm trying to read a file character by character. When I write the
    file out, there is one extra character
    I bet you're using feof (wrongly).
    which shows on the screen as a
    solid circle with a small question mark in the middle.
    >
    Here is what I have:
    >
    infile = fopen("encrypte d.txt", "r");
    outfile = fopen("plain.tx t", "w");
    What if either (or both) of these files fails to open?
    >
    while(!feof(inf ile)) {
    ch = fgetc(infile);
    fputc(ch, outfile);
    }
    >
    fclose(infile);
    fclose(outfile) ;
    >
    Can someone please tell me why this character is getting added to the
    outfile?
    feof is reactive, not predictive. It won't yield a non-zero result until
    you've actually tried to read past the end of the file.

    The fix is to stop using feof (or at least, to stop using it in quite that
    way):

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    if(argc == 3)
    {
    FILE *infile = fopen(argv[1], "r");
    if(infile != NULL)
    {
    FILE *outfile = fopen(argv[2], "w");
    if(outfile != NULL)
    {
    int ch;
    while((ch = getc(infile)) != EOF)
    {
    putc(ch, outfile);
    }
    if(ferror(infil e))
    {
    fprintf(stderr, "error reading %s\n", argv[1]);
    }
    if(ferror(outfi le))
    {
    fprintf(stderr, "error writing %s\n", argv[2]);
    }
    if(fclose(outfi le) != 0)
    {
    fprintf(stderr, "error closing %s\n", argv[2]);
    }
    }
    else
    {
    fprintf(stderr, "error opening %s\n", argv[2]);
    }
    fclose(infile);
    }
    else
    {
    fprintf(stderr, "error opening %s\n", argv[1]);
    }
    }
    else
    {
    fprintf(stderr, "please specify input and output filenames\n");
    }

    return 0;
    }

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Peter Nilsson

      #3
      Re: extra character when writing file

      Jim wrote:
      Hi There,
      >
      I'm trying to read a file character by character. When I write the
      file out, there is one extra character which shows on the screen as a
      solid circle with a small question mark in the middle.
      >
      Here is what I have:
      >
      infile = fopen("encrypte d.txt", "r");
      outfile = fopen("plain.tx t", "w");
      >
      while(!feof(inf ile)) {
      ch = fgetc(infile);
      fputc(ch, outfile);
      }
      >
      fclose(infile);
      fclose(outfile) ;
      >
      Can someone please tell me why this character is getting added to the
      outfile?
      This is a FAQ...



      --
      Peter

      Comment

      • Jim

        #4
        Re: extra character when writing file

        while((ch = getc(infile)) != EOF)
        {
        putc(ch, outfile);
        }
        Thank you that was very helpful.

        I'm hoping you can clarify for me... I'm curious why the extra ()
        around the ch = getc(infile). Would that not return a true/false and
        if so, why does that not trigger the EOF??

        Also: Why are you using getc instead of fgetc??

        Comment

        • CBFalconer

          #5
          Re: extra character when writing file

          Jim wrote:
          >
          I'm trying to read a file character by character. When I write the
          file out, there is one extra character which shows on the screen as
          a solid circle with a small question mark in the middle.
          >
          Here is what I have:
          >
          infile = fopen("encrypte d.txt", "r");
          outfile = fopen("plain.tx t", "w");
          >
          while(!feof(inf ile)) {
          ch = fgetc(infile);
          fputc(ch, outfile);
          }
          >
          fclose(infile);
          fclose(outfile) ;
          >
          Can someone please tell me why this character is getting added to the
          outfile?
          Yes. You are misusing feof. You are displaying EOF, which isn't a
          char. See the cfaq (see listing below). Your code should read:

          #include <stdio.h>

          int main(void) {
          int ch; /* note that this is an int */
          FILE infile, outfile; /* You need to declare vars you use */

          infile = fopen("encrypte d.txt", "r");
          if (!infile) {
          puts("Can't open 'encryped.txt'" );
          return 0;
          }
          outfile = fopen("plain.tx t", "w");
          if (!outfile) {
          puts("Can't open 'plain.txt');
          fclose(infile);
          return 0;
          }
          /* files all successfully opened */
          while (EOF != (ch = getc(infile))) putc(ch, outfile);
          fclose(infile);
          fclose(outfile) ;
          return 0;
          }

          And I haven't made you use stdlib and EXIT_SUCCESS or
          EXIT_FAILURE. But everything is checked except the fclose calls
          and puts calls (and the putc call).

          --
          Some useful references about C:
          <http://www.ungerhu.com/jxh/clc.welcome.txt >
          <http://c-faq.com/ (C-faq)
          <http://benpfaff.org/writings/clc/off-topic.html>
          <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
          <http://cbfalconer.home .att.net/download/n869_txt.bz2(C9 9, txt)
          <http://www.dinkumware. com/c99.aspx (C-library}
          <http://gcc.gnu.org/onlinedocs/ (GNU docs)
          <http://clc-wiki.net/wiki/C_community:com p.lang.c:Introd uction>

          ** Posted from http://www.teranews.com **

          Comment

          Working...