Redirection in command line "<"
-
Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)
-
Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)
uus831 wrote:
But, what happens when i do program1.exe < input.txt?
stdin is changed from the keyboard to input.txt To read the data you can use anything that reads from stdin line scanf(), getch() etc
uus831 wrote:
Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt)
Because the < input.txt is not an argument to your application, its an instruction to the os to modify what stdin points to
-
Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)
Note that stdin and stdout are also file handles. When no redirection occurs, stdin is the keyboard and stdout is the monitor. In the form of
program1.exe > out.txt
, stdout is assigned with a file handle to the file out.txt, instead of the monitor. So all the output goes into out.txt instead of the monitor. In the form ofprogram1.exe < in.txt
, stdin is assigned with a file handle to the file in.txt, instead of the keyboard. So whenever you read from stdin, e.g. when you call the scanf() function, the data come form in.txt, not the keyboard. Redirecting is handled by the OS, not by the program, so the program itself will know nothing about the redirection, and redirection directives would be passed to program as parameters. Thus argc in main is 1, not 3. -
Note that stdin and stdout are also file handles. When no redirection occurs, stdin is the keyboard and stdout is the monitor. In the form of
program1.exe > out.txt
, stdout is assigned with a file handle to the file out.txt, instead of the monitor. So all the output goes into out.txt instead of the monitor. In the form ofprogram1.exe < in.txt
, stdin is assigned with a file handle to the file in.txt, instead of the keyboard. So whenever you read from stdin, e.g. when you call the scanf() function, the data come form in.txt, not the keyboard. Redirecting is handled by the OS, not by the program, so the program itself will know nothing about the redirection, and redirection directives would be passed to program as parameters. Thus argc in main is 1, not 3. -
Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
uus831 wrote:
But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
To read file, I suggest you not to use DOS-shell indirection (< ) but use program arguments instead. Such as:
MyTest.exe -i "sample.txt"
or
MyTest.exe /i "sample.txt"In the above example, your application handles the command-line arguments and gets the file name(s). Then you can use
fstream
to load the content of the whole file(s), or to read part of the file(s).Maxwell Chen
-
Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
As the Windows built-in tool,
fc.exe
, it compares two files. The command for using this tool is as:fc /b C:\Temp\hello.txt D:\Doc\hello_2.txt
Maxwell Chen
-
uus831 wrote:
But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
To read file, I suggest you not to use DOS-shell indirection (< ) but use program arguments instead. Such as:
MyTest.exe -i "sample.txt"
or
MyTest.exe /i "sample.txt"In the above example, your application handles the command-line arguments and gets the file name(s). Then you can use
fstream
to load the content of the whole file(s), or to read part of the file(s).Maxwell Chen
-
Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
uus831 wrote:
But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times?
fscanf()
will tell you when it has read past the end of the file stream:while (fscanf(stdin, ...) != EOF)
{
...
}"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne