0

So I understand that to read and print out a line of text you can just use printf, scanf and type it out. However what if I want to print out a text file without typing them out in terminal? And I don't mean using fopen(filename, "r") where you can only open a specific file. I think this is called redirection but I'm having trouble understanding it. Something along the line as the below input example:

./myprogram < input.txt
9
  • Do you mean that every time your program asks for input, it will get it from the "input.txt" file? Commented Sep 15, 2019 at 10:04
  • not specifically that file but whatever is after the "<"
    – oog1119
    Commented Sep 15, 2019 at 10:14
  • Not sure if I understand you, you want to read whatever is at the right of the < sign? if so, you can use fread to read from stdin.
    – stylo
    Commented Sep 15, 2019 at 10:16
  • @stylo yes that is correct but how?
    – oog1119
    Commented Sep 15, 2019 at 10:22
  • in the fread function, the last argument is the stream you're specifying, just call it with fread(your_buffer, buffer_size, 1, stdin);
    – stylo
    Commented Sep 15, 2019 at 10:24

2 Answers 2

1

Here is a redirection cheat sheet. The line that interest us is:

cmd < file: Redirect the contents of the file to the standard input (stdin) of cmd.

Here a simple example that will print the content of your input.txt file. Compared to manual input, the program will never wait and will loop until the end of the file is reached (Note: there are cases where there is no end, you might want to add alternative break condition).

#include <stdio.h>

int main(void)
{
    char    buffer[100];

    while (fgets(buffer, 100, stdin))
        printf("%s", buffer);
    return (0);
}
  • ./myprogram < input.txt will print your input.txt
  • ./myprogram will wait for your manual input and print what you just typed.
1
  • yeah Ive tried that but for some reason its not printing any of the lines out, when I did it without the while loop i.e only fgets(buffer, 100, stdin), it only prints out the first line of the text file
    – oog1119
    Commented Sep 15, 2019 at 10:37
0

This is not exactly what you asked but you can put the filename as argument and get it in argv[1] and then use fopen

3
  • That's exactly the opposite of what they're asking. Commented Sep 15, 2019 at 10:20
  • 1
    yes because he doesn't want to have a fixed filename but using argv resolves the problem
    – Imu Sama
    Commented Sep 15, 2019 at 10:21
  • Re-reading the question I'm even more confused. I mean, you're not wrong if they're asking how they can open a file passed from the command line. But it's not clear to me that that's what they're asking. They threw input redirection into the mix and I have to wonder if they're asking about that instead. Commented Sep 15, 2019 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.