0

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?

Dark
  • 3
  • 1
  • It's referred to via `stdin` or file descriptor `0`. – Cheatah Oct 12 '22 at 13:59
  • In this program, invoked that way, you can read from `stdin`, and you'll get input from that file. It'll be the same as if you had explicitly said `FILE *ifp = fopen("seznam.txt", "r");`, or maybe `FILE *ifp = fopen(argv[1], "r");`, and then read from `ifp`. – Steve Summit Oct 12 '22 at 14:11
  • Similar question: try invoking `outDebug.exe "hello world"`, and ask, where did the quotes go? – Steve Summit Oct 12 '22 at 14:14
  • If you want your program to access the file `seznam.txt` directly, simply invoke `outDebug.exe seznam.txt`. – Steve Summit Oct 12 '22 at 14:15
  • @SteveSummit is it possible to do this without fopen()? As in directly working with the file, and not it's file name from which you get the file? – Dark Oct 12 '22 at 14:29
  • @Dark I think the answer to your question is "yes". As we've been saying, if you use the ` – Steve Summit Oct 12 '22 at 14:36
  • 2
    The whole point of redirection is that the program doesn't have to do anything. It just reads from standard input normally. If input is redirected, it reads from the file. If not, it reads from the terminal. – Barmar Oct 12 '22 at 14:45

1 Answers1

0

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;
}
CGi03
  • 452
  • 1
  • 3
  • 8