Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FILE open question

Status
Not open for further replies.

mrkan

Programmer
Oct 30, 2006
39
US
I am calling c program like this:

Code:
myprog < text.txt

How do I handle content of text.txt in my c program?
I could use gets but I need to use FILE pointer:

FILE *fp

and somehow to make this fp to point to text.txt

Is this possible?

Thanks

 
When you run the program like that, then the contents of the file appear on stdin

So
Code:
char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
}
will process the whole file.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
stdin is a FILE*

scanf(fmt, yyy);

is the same as

fscanf (stdin, fmt, yyy);

If you need to use fp, assign stdin to it

fp = stdin;
 
And [tt]stdin[/tt], [tt]stdout[/tt], and [tt]stderr[/tt] are all already open by default when the process starts. You don't need to open them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top