Like Tree1Likes
  • 1 Post By std10093

Having trouble with fgetc

This is a discussion on Having trouble with fgetc within the C Programming forums, part of the General Programming Boards category; Hi. I've been doing the C tutorial on this site this week, and I'm currently on the File I/O page. ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Having trouble with fgetc

    Hi. I've been doing the C tutorial on this site this week, and I'm currently on the File I/O page. I made a small program to see if I understood the fgetc function correctly, but it's not working correctly, and I could use some help to understand what I'm doing wrong.

    Here is the code:
    Code:
    #include <stdio.h>
    
    FILE *ptr;
    char number[50];
    int i = 0;
    
    int main(){
    ptr = fopen ( "c:\\test.txt", "r" );
    
    int c;
    while ( ( c = fgetc ( ptr ) ) != EOF ){
            number[i] = fgetc ( ptr );
            printf ( "%d\n", c );
            printf ( "%d\n", i );
            printf ( "%c\n\n", number[i] );
            ++i;
    }
    printf ( "%d\n", c );
    fclose ( ptr );
    }
    In test.txt it says "ABCDEFGHIJKLMNOPQRS". My idea was to copy the text to the string "copied". I'm printing the information in the variables and string for each run of the loop so I can get a better look at what's going on.

    Here's the result:
    Code:
    65
    0
    B
    
    67
    1
    D
    
    69
    2
    F
    
    71
    3
    H
    
    73
    4
    J
    
    75
    5
    L
    
    77
    6
    N
    
    79
    7
    P
    
    81
    8
    R
    
    83
    9
     
    
    -1
    
    Process returned 0 (0x0)   execution time : 0.016 s
    Press any key to continue.
    It's only copying every other character in the text, skipping half of them.
    The number returned by fgetc does not represent the character it should. In the first run, it returns 65, which should be an A, but a B gets stored in the string.
    In the last run, it returns 83, but nothing gets stored in the string at all.

    I've spent an hour trying to figure this out, and I'd be grateful if someone could shed some light on it.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    So you see that your output is skiping one letter per loop....
    Why?
    How many times do you call the fgetc function inside the loop? What will fgetc function will do every time that gets called?
    Also there is a variable c there.....It might be useful for you
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Aah, I see what you're getting at. When fgetc is used, it returns a character, and the next time it's used it will return the character placed after the last one returned. I'm using the fgetc function twice, once for checking if the condition for the loop is true, and again for copying it to the string. When it's checking the condition, at the start of each loop, the returned character doesn't get saved in the string, therefore leaving me with only half of the characters in the string.

    If I use the variable "c" to check the condition, fgetc will only be used once in each loop, and every character will be saved.

    Here's my updated loop, it's now working properly.
    Code:
    int c;
    while ( c != EOF ){
        copied[i] = fgetc ( ptr );
        c = copied[i];
        printf ( "%d\n", c );
        printf ( "%d\n", i );
        printf ( "%c\n\n", copied[i] );
        ++i;
    }
    Thank you for the help

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    But know you are getting in a situation where you checking if an uninitialized variable is equal to EOF..You do not want that

    Again to be sure that everything is clear . When calling fgetc twice, the first call will get you the first letter A and store it into c...Then in the second call the file pointer is moved to next letter, so you will read B...But you want in every loop to read one letter and store it

    So, we have to call it once...You solved this issue, but now you compare an uninitialized value to EOF.. You could randomly initialize c and compare but i would strongly recommend you to do the loop like this
    Code:
    #include <stdio.h>
     
    FILE *ptr;
    char number[50];
    int i = 0;
     
    int main(){
    ptr = fopen ( "c:\\test.txt", "r" );
     
    int c;
    while ( ( c = fgetc ( ptr ) ) != EOF ){
            number[i] = c;
            printf ( "%d\n", c );
            printf ( "%d\n", i );
            printf ( "%c\n\n", number[i] );
            ++i;
    }
    printf ( "%d\n", c );
    fclose ( ptr );
    }
    I just went in your code in first post and replace the 2nd call of fgetc with the variable c.That will work because in the while line of code, we assign what fgetc returns to us to c

    You are welcome.Also welcome to the forum

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Just to be sure I understand this correctly, by uninitialized variable you mean a variable without a known value, and since I don't give it a value until after the while condition is checked, there's a chance that the value actually does equal EOF, and the loop won't run?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Exactly.Well i guess that if we calculate the possibility of this to happen, it will be small, but you do not your code to has unexpected behavior in any case.Remember that as a general rule

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    I will. Thanks again

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,243
    You might also want to consider what that code will do if the input file contains more than 50 characters. Reading into an array is fine (assuming you intend to extend the code to use the array) but it is a good idea to avoid reading 51 or more characters into an array with 50 elements.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use fgetc
    By a_satari in forum C Programming
    Replies: 9
    Last Post: 03-03-2011, 10:11 AM
  2. fgetc help
    By Cali_engr in forum C Programming
    Replies: 2
    Last Post: 05-17-2010, 09:07 PM
  3. fgetc and EOF
    By pic-o-matic in forum C Programming
    Replies: 18
    Last Post: 08-29-2008, 09:08 AM
  4. help with fgetc
    By agentsmith in forum C Programming
    Replies: 3
    Last Post: 04-15-2008, 10:08 AM
  5. fgetc().....Don't Quite Get This:
    By Krak in forum C Programming
    Replies: 7
    Last Post: 09-24-2005, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21