Thread: reading the number of characters in a file

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    24

    reading the number of characters in a file

    Hello everybody, I am having a problema with a simple program that is supposed to find the number of characters in a file but apparently it gives me n+1 elements instead of just n. Here is the code, hope you can help me.
    Code:
    #include <stdio.h>
    int main (void)
    {
    FILE *fp;
    char c;
    int d=0;
    fp=fopen("ex4.txt","r");
    while(c!=EOF)
    {
    c=fgetc(fp);
    d=d+1;
    }
    fclose(fp);
    printf("The number of characters is %d\n",d);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem is probably your while condition. Using EOF to control an input loop usually leads to an extra extraction. Why not use the actual read to control the read loop?

    You also need to find and use some kind of indentation style, it'll make reading your program much easier.


    Jim

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    24
    Hello, thank you for your answer, I'll try to make my code readable next time. How can i make that while you're talking of? I cannot find the solution here...

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    In C, you do not whether you are at the end of a file (or any input stream), until you try to read past the end.

    Think about the above sentence, until you understand its implications.

    Quote Originally Posted by khanny View Post
    Code:
        while (c != EOF) {
            c = fgetc(fp);
            d = d+1;
        }
    The above loop detects the end of file at the second line, when fgetc(fp) returns EOF.

    That EOF is not a character -- it is not "the last character in the file", or even an end-of-file character; it is just a special value that many read functions, including fgetc() , return when they detect that there is no more input.

    (I guess it is okay to think of it as end of file mark, but just remember that it is not actually saved in the file or file system, nor does it describe any real character at all; it is just a synthetic value the C library provides you as a notification for the end-of-input case.)

    Now, consider this: Does your above loop count c == EOF as a character in the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-22-2015, 11:04 PM
  2. Trouble counting number of characters copied to file
    By Tripswitch in forum C Programming
    Replies: 13
    Last Post: 07-22-2014, 08:15 AM
  3. File I/O reading in characters and numbers
    By whiteboyfly in forum C Programming
    Replies: 3
    Last Post: 12-01-2013, 05:27 PM
  4. reading characters from a file
    By nmn in forum C Programming
    Replies: 2
    Last Post: 03-19-2013, 12:29 PM
  5. reading characters in a file
    By jane in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 07:10 AM