A huge huge thanks for all the assistance in my previous thread. It has me well on my way. The problem right now is I am trying to read input from a file using the '<' redirection.

I however, am completely lost on how to do this. Searching google has yielded tons of results for the normal '>' redirection (and it's many variants) but no clear example on how to do this with C.

I figured out that this redirection is considered input from a console, meaning I can't just consider the file contents to be command line arguments (and therefore use the argv [] in the main), so that dream died quickly.

How can I read input from a file into my C executable? If anybody could provide a very basic example of this I would be so incredibly grateful!

Recommended Answers

All 4 Replies

This doesn't have anything to do with C, but your shell. For example:

$ some_program < input_file

When some_program reads from stdin, it will be reading the contents of input_file.

commented: Indeed +29

indeed, this is the first intuitive step, but my problem is i'm not sure how to handle this redirection.

I mean... does it just substitute an answer for the first time the script hits a scanf function?

Do I have to take any extra measures to make sure the redirection is handled properly?

>does it just substitute an answer for the first time the script hits a scanf function?
That's one way of thinking of it. 'stdin' becomes a file handle to the input file. The same rules apply when reading from stdin as when reading from a regular file. So, if you were to run scanf("%s", mystring); it would attempt to read a string from the input file and reposition the file pointer accordingly. If your program attempts to read data when you've reached the end of the file, you'll get an EOF.

>Do I have to take any extra measures to make sure the redirection is handled properly?

Let's say you want to read some input and display it.

#include <stdio.h>

int main(void)
{
	int ch;

	/* read character by character from stdin */
	do {
		ch = fgetc(stdin);
		putchar(ch);
	} while (ch != EOF); 

	return 0;
}

Nothing extraordinary, right?
Now, the redirecting part works like this.
If your program is called redirect and you run it at the command line as redirect alone; a prompt will wait until you start entering data, and as soon as you press enter it will show in the screen or stdout. In order to stop it you'll have to press Ctrl+Z in windows or Ctrl+D in Unix (this is equivalent of EOF).
However if you run your program as: redirect < textfiletest.txt it'll display whatsoever is in the textfile.txt
Furthermore if you run it as: redirect < textfiletest.txt > anothertextfile.txt. it'll display to another file and nothing will be displayed on screen.

Also you can even pipe it as:
echo "This is a text line test" | redirect or echo "This is a text line test" | redirect > putintofile.txt

commented: All you should need to know about redirection - excellent +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.