'User input through terminal with input file

So I calculated the mean and standard deviation from double values read within a file.

My file data has 1 number per line: My data in the file is the following

1
2
3
4
5
6
7
8
9
10

My code is below:

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

int main(){

    FILE *inputfile= fopen("file.dat.txt", "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j = i*i;
        stdreal +=j;
    }
        average = sum/x;
        stdish = (stdreal/x)-(average*average);
        stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);

 fclose(inputfile);
return 0;
}

I am running this through the terminal. My data file is file.dat.txt. What I am trying to do is to have the user input the text file through the terminal instead of having it in the program.

Like this: ./sdev < file.dat

I'm not exactly sure how to implement this in my program...

Thanks!



Solution 1:[1]

write the main function with argument. So that you can able to pass file name from terminal while invoking the executable file.

See the example program in this path: http://www.astro.umd.edu/~dcr/Courses/ASTR615/intro_C/node11.html

Solution 2:[2]

There are basically two ways to do that. The first approach is to accept input via stdin by file redirection. This is the approach you proposed:

$ ./sdev < file.dat

Your program then should read from stdin instead of inputfile. You can change all occurrences of ´inputfiletostdin, but you could also makeinputfilea shallow copy to the file handlestdin`:

int main()
{    
    FILE *inputfile = stdin;

    /* do stuff */
    return 0;
}

Note that you don't close stdin. It is a system file handle that is managed by the operating system. It is not NULL.

The second approach is to specify the file as command line argument:

$ ./sdev file.dat

Your program must then use the form of main that accepts command line parameters. Note that the program name itself is passed as the first parameter, so your file name should be in ´argv[1]`:

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

    if (argc != 2) {
        fprintf(stderr, "Usage: sdev inputfile\n");
        exit(1);
    }

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

    if (inputfile == NULL) {
        fprintf(stderr, "Failed to open %s.\n", inputfile);
        exit(1);
    }

    /* do stuff */

    fclose(inputfile);
    return 0;
}

Here, you have to open and close inputfile explicitly in your program.

Solution 3:[3]

you can do it with the argument passing to the program with ./sdev file.dat

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

    if (argc != 2 )
    {
        printf("Your help text here\n");
    }

    FILE *inputfile= fopen(argv[1], "r");

    [...]

or if you want to read it with echo "file.dat" | ./sdev you have read it with stdin this could you then extend to read in multiple files.

char filename[1024]
if (fgets(filename, sizeof(filename), stdin)) {

    FILE *inputfile= fopen(filename, "r");

    [...]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mahendiran.b
Solution 2 M Oehm
Solution 3