Thread: Super simple - Opening/Reading a file; Don't get it

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    49

    Super simple - Opening/Reading a file; Don't get it

    I don't know what the condition for my while loop needs to be. I just want it to read over a file of numbers (800+) and print those numbers out to the user.

    Code:
    #include <stdio.h>
    #define ALL_NUMS 800
    
    main(void)
    {
      FILE *fptr;
      char c;
      char file_name[20];
      int i, j;
    
      printf("Please enter the name of the file containing the set of numbers you wish to sort.\n");
      scanf("%s",file_name);
    
      fptr=fopen(file_name,"r");
    
      if(fptr == NULL){
        fprintf(stderr, "Can't open input file.\n");
        exit(1);
      }
    
      while(){
        c=fgetc(fptr);
        printf("%c",c);
      }
    
      fclose(fptr);
    
      return 0;
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    Does putchar automatically cast the int to a char?

    Code:
    int c;
      
    while ((c = fgetc(fp)) != EOF)
    {
      putchar (c);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by new2C- View Post
    Does putchar automatically cast the int to a char?
    What makes you think putchar expects a char? (Well, besides the name.)

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    I was thinking it did due to the name. I noticed in the prototype it takes an int parameter, I also noticed the examples in main have some parameters passed as a char, shouldn't that create some warning from the compiler of incompatible data types?

    Example:

    Code:
    #include <stdio.h>
    
    int main ()
    {
       char ch;
    
       for(ch = 'A' ; ch <= 'Z' ; ch++) {
          putchar(ch);
       }
       
       return(0);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    C has a notion of automatic type promotion, which means that (for instance) passing a char to a function that expects an int will automatically (and silently) convert the char to an int (of the same value). (char and short can be promoted to int, and I believe all of those can be promoted to long int; similarly float can be promoted to double).

    As an extra fun fact that you didn't want to know about C, you might get warnings from the above, but on line 7 because 'A' (and any character constant) is of type int, not of type char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-13-2012, 01:42 PM
  2. Best practice for super simple program
    By samwillc in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2012, 04:29 PM
  3. Opening, Reading and Updating a text file.
    By bonett09 in forum C Programming
    Replies: 8
    Last Post: 12-25-2011, 06:03 AM
  4. Help on a simple "opening file" program :/
    By sepuku in forum Linux Programming
    Replies: 7
    Last Post: 12-14-2010, 05:26 PM
  5. help w/structure array and opening reading file
    By live4soccer7 in forum C Programming
    Replies: 7
    Last Post: 03-15-2009, 10:50 PM