How to Feed Input Files to C Program
-
03-10-2014 #1
Registered User
How to input test.txt into c program?
if i have a program named prog.c and i have already converted it into an executable using gcc
how do i test the prog.o in linux? what is the command for executing a test file where i have my inputs? so that at ./prog.o what extar command needs to be added with a test.txt fiel for inputs?
so if i have to input some numbers and it is supposed to test those inputs and exit on fail?
-
03-10-2014 #2
C++ Witch
That depends on how you are reading input in that program. For example, if you are reading from standard input, then you could redirect input from the file to the program (or directly enter the test input yourself). If you wrote the program to take a file name as a command line argument and then open the file with that name, then you would run the program with the file name as a command line argument. Or, maybe your program prompts for a file name, upon which you would enter the file name as input when running the program, and then your program opens the file with that name.
Originally Posted by Bjarne Stroustrup (2000-10-14) Look up a C++ Reference and learn How To Ask Questions The Smart Way
-
03-10-2014 #3
Registered User
yes but what i am trying to do is feed the porgram a txt file with numbers to test out ,using linux commands
-
03-10-2014 #4
and the hat of int overfl
If your program reads standard input, then
./a.out < input.txt
will work.Similarly, you can save all the output as well with
./a.out < input.txt > output.txtIf your program accepts arguments, and tries to open a file for say argv[1], then use
./a.out input.txt
-
03-11-2014 #5
Registered User
thank you this is exactly what i was looking for
Originally Posted by Salem
now i would like to extend the question just to understand this clearly
if this is my program(hypothetically speaking)
and i want my program to keep scanning the test.txt file until it reaches either END OF FILE or it breaks because it couldn't confirm the year was after 1528(which for me means a fail)Code:
#include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]){ int year; scanf("%d",year); if(year>1528) do_something else return 0; return EXIT_SUCCESS; }i know i need to include the END OF FILE and also a while loop, but where exactly?
-
03-11-2014 #6
and the hat of int overfl
Ignore the fact that you're reading from a file, how would you detect EOF from the terminal?
Hint: read the manual page for scanf and find out what it returns.
-
03-14-2014 #7
Registered User
it says in the reference card that when it reaches EOF that it returns this in the int return of scanf
Originally Posted by Salem
so should i be doing "while(scanf(....);!=EOF);"
-
03-14-2014 #8
Registered User
-
03-15-2014 #9
Registered User
I believe so, no!
Originally Posted by new2C-
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Source: https://cboard.cprogramming.com/c-programming/162086-how-input-test-txt-into-c-program.html
0 Response to "How to Feed Input Files to C Program"
Post a Comment