How to use fgetc

This is a discussion on How to use fgetc within the C Programming forums, part of the General Programming Boards category; Hi. Think I'm having trouble with fgetc. I'm trying to read from a text file containing integers and write to ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    How to use fgetc

    Hi. Think I'm having trouble with fgetc.
    I'm trying to read from a text file containing integers and write to an
    integer array.

    The text file contains: 9876 54321
    the output from the array returns : 2289536

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
       /*Initialisations*/
       FILE *fin;
       char c;
       char file_name_in[20];
       int int_array[1000];
    
       /*Enter file name*/
       printf("enter the file name\n");
       scanf("%s",file_name_in);
    
       /* test to see if file opens */
       fin = fopen(file_name_in, "r");
       if (fin == NULL){
          (void)fprintf(stderr,"Cannot open input file %s\n",file_name_in);
       }
    
       int i=0;
    
       while((getc(fin))!=EOF){
          int_array[i] = fgetc(fin);
          i++;
       }
    
       printf("%d",int_array);
    }
    thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You know fgetc() reads character and return you character or EOF if end of file is encountered.

    fgetc/getchar() returns EOF. Question 12.1


    Code:
          int_array[i] = fgetc(fin) - '0';
    should work
    Still seems like you don't know what you are doing.

    printf("%d",int_array);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by a_satari View Post
    Hi. Think I'm having trouble with fgetc.
    I'm trying to read from a text file containing integers and write to an
    integer array.

    The text file contains: 9876 54321
    the output from the array returns : 2289536
    I take it you want the array to contain integers equal to the two numbers in the file... In that case you need to use fscanf() ... with "%d" in the formatting string.

    You would also be well advised to make your file_name_in buffer a fair bit bigger. In Windows file paths can reach 256 characters, longer in Linux...

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Thanks, Baynit and CommonTater.

    Baynit: I tried your solution and got the same answer.
    In the line:
    Code:
    int_array[i] = fgetc(fin);
    fgetc gets the character and places it into int_array, which is an integer. Perhaps something goes wrong in conversion?

    CommonTater: Yes, that was my next step. To take the two numbers and place them into the array. My first step was to place individual integers into the array.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by a_satari View Post
    Perhaps something goes wrong in conversion?
    ...
    My first step was to place individual integers into the array.
    I take it you mean individual digits, not integers? So given the sample input from your first post, the array would contain: [9][8][7][6]....

    Part of the problem is that you read in a character, meaning that when fgetc reads the digit 9, it gives you the ASCII value for the character '9'. You need to normalize this to an actual integer. You can do this with something like int_array[i] = fgetc(in) - '0'.

    Code:
       printf("%d",int_array);
    This is the other part of your problem. You can't print a whole array in one printf statement. You need to use a loop to print each element individually. When you pass int_array to a function, it simply gives the address of the start of the array. That means the number that printf spits out is the address of int_array, not the elements inside.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Bangalore
    Posts
    1
    char *name = "Raju";
    I know this is a constant string, i can't change a character inside the string when i m working in unix, But i can change in Turbo C. How it is possible......... please tell me if u know

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Thanks anduril462.
    I think your right about spitting out the address of int_array.
    I've changed my code to match what you've and Bayint have said:
    Code:
       while((getc(fin))!=EOF){
          int_array[i]= fgetc(fin) - '0';
          i++;
       }
       for (i=0;i<=10;i++){
          printf("%d",int_array[i]);
       }
    but still get incorrect output. Any ideas?
    Last edited by a_satari; 03-03-2011 at 04:18 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by Manjunath H View Post
    char *name = "Raju";
    I know this is a constant string, i can't change a character inside the string when i m working in unix, But i can change in Turbo C. How it is possible......... please tell me if u know
    Manjunath, hijacking somebody else's thread is very rude. It's just like interrupting them while they are in the middle of talking. Please start your own thread for this question, and somebody will help you.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by a_satari View Post
    Thanks anduril462.
    I think your right about spitting out the address of int_array.
    I've changed my code to match what you've and Bayint have said:
    Code:
       while((getc(fin))!=EOF){
          int_array[i]= fgetc(fin) - '0';
          i++;
       }
       for (i=0;i<=10;i++){
          printf("%d",int_array[i]);
       }
    but still get incorrect output. Any ideas?
    You are calling getc in your while loop condition, then calling fgetc in the loop body. That reads two characters from the stream, but you only store one in the array. Just use fgetc once in the loop condition, like so:
    Code:
    int c;
    while ((c = fgetc(fin)) != EOF) {
        // use isdigit() to check if c is a digit, and if so, assign it to the array (subtract '0')
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    A couple other quick notes:
    1. You said main would return an int, so make it do so. A return 0 at the bottom should be fine.
    2. There's no need to cast the return value of fprintf, unless you have an outdated compiler that whines about it, in which case you should update. Pelles C seems to be a favorite around here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner- fgetc, arrays, and singly linked lists
    By timg123 in forum C Programming
    Replies: 5
    Last Post: 07-15-2010, 09:02 AM
  2. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  3. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 10:55 PM
  4. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  5. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 05:00 AM

Tags for this Thread


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