reading the content of a file from command line

Hello,
i would like to read the content of a text file data.txt (line by line ) directly from the command line using this command:
a.exe < data.txt.
What could be the c++ code to read/get the content of these lines (without using ifstream). The treatment of the lines is not a problem for me but i really don't know how to access the content of the file from the c++ code
thank you very much for your help.
I think you can use std::cin.
you dont need the '<', you could just type


a.exe data.txt


read this command line argument into a std::string
http://www.cprogramming.com/tutorial/lesson14.html


For reading text files see the 2nd example in the "Text files" section.

http://www.cplusplus.com/doc/tutorial/files/

but where he's hard-coded his file name:

ifstream myfile ("example.txt");

you want to use use your command line argument string argument.



edit: You will need to use the whole path when you run it. something like this:


a.exe "D:\path\to\file\file.txt"


OR get your program to work out where in the file system you are running your program.

Last edited on
Topic archived. No new replies allowed.