Sep 11, 2007 #1 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
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
Sep 11, 2007 #2 Salem Programmer Apr 29, 2003 2,455 GB 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. Upvote 0 Downvote
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.
Sep 12, 2007 #3 xwb Programmer Jul 11, 2002 6,828 GB 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; Upvote 0 Downvote
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;
Sep 12, 2007 #4 SamBones Programmer Aug 8, 2002 3,186 US 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. Upvote 0 Downvote
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.