0

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
}
Arun A S
  • 6,421
  • 4
  • 29
  • 43
Cesar A
  • 663
  • 1
  • 7
  • 10
  • `while ((charHolder = getchar()) != EOF){` is good if `charHolder` is type `int`. `getchar()` returns a value in the range of `unsigned char` and `EOF`. These typically 257 different values cannot be stored uniquely in a `char`. – chux - Reinstate Monica May 01 '15 at 11:47

1 Answers1

0

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.

alexey
  • 126
  • 1
  • 6
  • does the whole code gets executed per each character? – Cesar A May 01 '15 at 10:34
  • Nope. To do something with each char replace "printf("%c", ch);" in do_read function with something you need to with "ch". – alexey May 01 '15 at 10:39
  • I am trying to append each character from a string, for example "ASD", and before returning 0, I created a method that prints the array... But the array is printed 3 times... Do you know why? – Cesar A May 01 '15 at 11:04
  • Look at this answer regarding "appending to array": http://stackoverflow.com/questions/10279718/append-char-to-string-in-c – alexey May 01 '15 at 11:13
  • Use `int ch`. Otherwise `if (ch == EOF)` will never be true if `char` is unsigned. It may be incorrectly true when `char` is signed and `ch ==255`. – chux - Reinstate Monica May 01 '15 at 11:40
  • Concerning "In case of binary files you also need to use feof function to check for EOF", check http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – chux - Reinstate Monica May 01 '15 at 11:44
  • Hey! Could you explain why you have while(1)? What does the '1' mean? whats that condition? – Cesar A May 01 '15 at 12:12
  • It always true, break statement will exit cycle – alexey May 01 '15 at 14:05