Search code examples
ccmdargumentsprogram-entry-point

How do I access an inputed file?


So I have this code:

  #include <stdio.h>

int main(int argc, char **argv) {
    //Reassign input arguments into local values
    // Check if inputs are valid
    // translate the input string
    //assign the list into a nested string

    //search for translated string in the list
    //save all found cases
    //print all found cases

    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    printf("%d",argc);
    return 0;
}

Which after typing: outDebug.exe hello <seznam.txt into the command prompt...

it gives me these returns:

argv[0] = outDebug.exe

argv[1] = hello

2

Where did the file go to if it's not in argv?


Solution

  • As mentioned in the comments, you can get it from stdin. With fgets for example:

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int i = 0;
        for (i = 0; i < argc; i++)
        {
            printf("argv[%d] = %s\n", i, argv[i]);
        }
        printf("%d\n",argc);
    
        char buffer[256];
        while(fgets(buffer, 256, stdin)) puts(buffer);
    
        return 0;
    }