fgetc

This is a discussion on fgetc within the C Programming forums, part of the General Programming Boards category; I just read about the fgetc function that reads a character (one by one) from a file. Well, I also ...

  1. #1
    Unregistered
    Guest

    fgetc

    I just read about the fgetc function that reads a character (one by one) from a file. Well, I also encountered this example code from the book and I need an explanation.
    Code:
    int main()
    {
        int counter = 0;  /* number of characters seen */
        FILE *in_file;    /* input file */
    
        /* character or EOF flag from input */
        int ch;
    
        in_file = fopen(FILE_NAME, "r");
        if (in_file == NULL) {
            printf("Cannot open %s\n", FILE_NAME);
            exit(8);
        }
    
        while (1) {
            ch = fgetc(in_file);
            if (ch == EOF)
                break;
            ++count;
        }
        printf("Number of characters in %s is %d\n",
                      FILE_NAME, count);
    
        fclose(in_file);
        return (0);
    }
    I'm not too sure if the rest of the code is right, but my question is in the while (1) loop. Won't "ch" just continuously look at the first character because what is telling it to move on? There is no incrementation. How is it reading anything but the first character in the file during every iteration? Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    After fgetc has read a character, some invisible (to you) pointer advances to the next character in the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    The same "unregistered" guy with the question. I was just looking at the code. How can you assign a char to an int variable ("ch")?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Maybe this might help

    Code:
    int main(){
      int counter=0;  /* number of characters seen */
      FILE *in_file;    /* input file THIS IS ACTUALLY A FILE STREAM */
      /* character or EOF flag from input */
      int ch;
      in_file = fopen(FILE_NAME, "r");
      if (in_file == NULL) {
      /*THIS IF STATEMENT IF THE FILE SUCCESSFULLY OPENED IF NOT IT TELLS THE USER AND EXITS*/
        printf("Cannot open %s\n", FILE_NAME);
        exit(8);
      }
      while (1) {
        /*WHY AN INFINITE LOOP I DON'T KNOW THERE IS EASIER WAYS*/
        ch = fgetc(in_file);/*READS ONE CHARACTER FROM FILE STREAM*/
        if (ch == EOF)/*IF 'ch' IS EQUAL TO END OF FILE (EOF) IT BREAKS FROM THE LOOP*/
          break;
        counter++;/*COUNTS NUMBER OF CHARACTERS*/
      }/*End of While Loop*/                             printf("Number of characters in %s is %d\n",
              FILE_NAME, count);                      /*PRINTS NUMBER OF CHARACTERS OBVIOUSLY*/   fclose(in_file); /*CLOSE FILE STREAM IF U DON'T IT MAY CAUSE ERRORS WHEN OPENING IT LATER*/                     
     return (0);/*EXIT PROGRAM*/
    }
    *****************************************
    AN EASIER WAY TO DO THE LOOP....

    Code:
    /*Just the loop part*/
    while(!feof(in_file)){
      ch=fgetc(in_file);
      counter++;
    }
    I also want to mention to your second post, apparently (I was told) that C interprets everything as numbers so if you print 65 as an integer it prints 65, if you print it as a character you get 'A'. This code may help you understand.

    Code:
    #include <stdio.h>
    
    int main(){
      int num;
      printf("Enter NUMBER:  ");
      scanf(" %d", &num);
      printf("\"num\" printed as integer is %d\n",num);
      printf("\"num\" printed as char is %c\n",num);
      return 0;
    }
    Hope this is helpful

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    ooh I better add...

    I better add that, the program i posted for entering the number only works with PRINTABLE ASCII CHARACTERS they are numbered from 32 (Which is space), 33 is !, right up to 126 is ~ you could write a program using a for loop to see all of them if you wish. This also may help you understand a bit.

    Also you may change the int to a char but make sure you changed the scanf("%d",&num); to scanf("%c",&num);
    Last edited by kwigibo; 09-10-2001 at 02:53 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Mmm

    This doesn't work
    Code:
    int ch;
    while(!feof(in_file)){
      ch=fgetc(in_file);
      counter++;
    }
    This does work
    Code:
    int ch;
    while( (ch=fgetc(in_file)) != EOF ){
      counter++;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 10:55 PM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  4. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 04:00 AM
  5. fgetc
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 03-03-2004, 08:18 AM

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