C - If (File not found) {Use standard input}

I have a program that reads from a file, but if there is no file in the arguments array, I would like to read from stdin in the terminal, like this:

 ./program.out test.txt

      

read from test.txt

 ./program.out

      

read from stdin:

here's my code for some context:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fr;
char *line;
char *word;
size_t len =256;
int i=0;
int sum=0;
char *vali;
const char delim = ' ';
int flag=0;

int main(int argc, char* argv[]){

line = (char *)malloc(len);
word = (char *)malloc(len);


/*
line = (char *)malloc(sizeof(&len));
word = (char *)malloc(sizeof(&len));
vali = (char *)malloc(sizeof(&len));
*/
    fr = fopen(argv[1], "r");
    if(fr==NULL){
        //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question
    }

        while (getline(&line, &len, fr) != -1){
            /* printf("%s", line ); */
            if(strlen(line) != 1){
                sscanf(line,"%s%*[^\n]",word);
                 printf("%-10s ", word); 
                char *scores = line + strlen(word) + 1;         
    /*          printf("scores: %s", scores); */



                vali=strtok(scores, &delim);
                while(vali != NULL){
                    sum=sum+atoi(vali);

                    vali = strtok(NULL, &delim);
                }

                printf("%3d\n", sum);
                sum=0;
            }
        }
        fclose(fr);

    free(line);
//   free(word);
//  free(vali);
    return 0;
}

      

+3


source to share


3 answers


Change these lines:

fr = fopen(argv[1], "r");
if(fr==NULL){
    //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question
}

      



to

if ( argc > 1 )
{
    // A file was passed in as the first argument.
    // Try to open it.
    fr = fopen(argv[1], "r");
    if(fr==NULL){
       // Deal with the error of not being able to open the file.
    }
}
else
{
    // Nothing was passed to the program.
    // use stdin.
    fr = stdin;
}

      

+8


source


How about simply:

if(fr==NULL){
    fr=stdin;

      



They are both pointers to files after all.

+2


source


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fr;
char *line;
char *word;
size_t len =256;
int i=0;
int sum=0;

      

This is already 0. 40 $ fine.

char *vali;

      

Non-English variable name. $ 50 fine.

const char delim = ' ';
int flag=0;

      

Using global variables for no reason is subject to a $ 10 / byte penalty. The inconsistent distance is $ 20.

int main(int argc, char* argv[]){

      

Inconsistent placement of '*'.

line = (char *)malloc(len);
word = (char *)malloc(len);

      

The malloc casting is $ 50. http://www.c-faq.com/malloc/mallocnocast.html

/*
line = (char *)malloc(sizeof(&len));
word = (char *)malloc(sizeof(&len));
vali = (char *)malloc(sizeof(&len));
*/

      

Consider #if 0 ... #endif instead.

    fr = fopen(argv[1], "r");

      

Inconsistent indentation.

Not checked if the argument was provided in the first place.

The standard behavior is to open stdin if "-" is passed as the name.

    if(fr==NULL){
        //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question

      

Wrong in its own right. Dropping back to stdin if the program failed to open the transferred file is a felony, especially since there is no indication.

You should check your arguments. If the file was provided and could not be opened, print an error .

Finally, there are many unix command line tools that use stdin by default and have sources available there (like cat), so you could just check what they do. Interestingly, there is a FILE pointer called stdin that you can use here.

    }

        while (getline(&line, &len, fr) != -1){

      

Even more incompatible indentation.

            /* printf("%s", line ); */
            if(strlen(line) != 1){

      

Why? Doesn't have it?

                sscanf(line,"%s%*[^\n]",word);

      

What? It really can be made much nicer. You know that the newline must be the last and the length is known.

                 printf("%-10s ", word); 
                char *scores = line + strlen(word) + 1;         
    /*          printf("scores: %s", scores); */



                vali=strtok(scores, &delim);
                while(vali != NULL){
                    sum=sum+atoi(vali);

                    vali = strtok(NULL, &delim);
                }

                printf("%3d\n", sum);
                sum=0;

      

Not a very good person. At least sum = 0; just before modifying it in the loop. Just a wandering sum = 0 makes you wonder what's going on here. 10 $.

            }
        }
        fclose(fr);

      

Finally, if thingy turns out to be stdin, this should not be done.

    free(line);
//   free(word);
//  free(vali);

      

Well, you are highlighting the word and the line, but not the vali, so the comment part is incorrect.

    return 0;
}

      

0


source







All Articles