Search code examples
carraysinputstdingetc

getc() for passed in input and file reading in C


I have to develop a program in C that can have two kinds of inputs.

  1. By feeding it a string ( I am assuming like this filename < String1234455678, please correct me if I am wrong).
  2. By reading data from some file(s).

I have to do some checks regarding the characters that are in it and store them in an array. But I want to learn how to use the getc() from stdin first.

My first question is, can I use getc() in both cases?

I wanted to loop through every single character in the feed line/file, and I assume the code would look something like this:

char Array1[];
char charHolder;


//If the file/feed has chars (!NULL), execute
if ((charHolder = getchar())!=NULL){
    //Do something
    //Do some more
    //Finally append to Array1
    Array1[] = charHolder;
}

There might be some issues with the code above. I wanted to know if that kind of inserting is valid in C (with no index specified, which it will just push the value at the end of the array). Also, I read from http://beej.us/guide/bgc/output/html/multipage/getc.html that getc(stdin) and getchar() are exactly equivalent. I just want to double check that this is indeed true and either function will work with both my cases where I have to read data (from a file and feeding my program a string).

Also, I was wondering how I can achieve reading characters from multiple files. Say if my program was to be executed as programName file1 file2.

Thank you for your time and help!

Cheers!

Edit 1:


I also wanted to know how to check when the chars end from a file/string feed. Should I use the EOF for both cases?

Example:

while ((charHolder = getchar()) != EOF){
    //code
}

Solution

  • Here is a sample:

    #include <stdio.h>
    
    void do_read(FILE * file, int abort_on_newline) {
        char ch;
    
        while (1) {
            ch = getc(file);
            if (ch == EOF) {
                break;
            }
            if (abort_on_newline && ch == '\n') {
                break;
            }
            printf("%c", ch);
        }
    }
    
    int main(int argc, char * argv[])
    {
        int i = 1;
        FILE * fp = NULL;
    
        if (1 == argc) {
            // read input string from stdin, abort on new line (in case of interactive input)
            do_read (stdin, 1);
        }
        else {
            // cycle through all files in command line arguments and read them
            for (i=1; i < argc; i++) {
                if ((fp = fopen(argv[i], "r")) == NULL) {
                    printf("Failed to open file.\n");
                }
                else {
                    do_read(fp,0);
                    fclose(fp);
                }
            }
        }
    
        return 0;
    }
    

    Use it like this:

    1. To read from stdin: echo youstring | youprogram, or just start yourprogram to get input from user
    2. To read from file(s) yourprogram yourfile1 yourfile2 ...

    Yes your can use getc in both cases, yes you should check for EOF in both cases, except for interactiv input. In case of binary files you also need to use feof function to check for EOF. See code above to read from multiple files.