For faster input, I read that you can do file-redirection and include a file with the cin inputs already set.
In theory it should be used like following:
App.exe inputfile outputfile
As far as I understood from C++ Primer book, The following C++ code[1] should be reading cin input from the text file and shouldn't need to any other special indication like[2]
[2]
include <fstream>
ofstream myfile;
myfile.open ();
[1] The following C++ code...
#include <iostream>
int main()
{
int val;
std::cin >> val; //this value should be read automatically for inputfile
std::cout << val;
return 0;
}
Am I missing something?
To use your code [1] you have to call your program like this:
App.exe < inputfile > outputfile
You can also use:
App.exe < inputfile >> outputfile
In this case the output wouldn't be rewritten with every run of the command, but output will be appended to already existing file.
More information about redirecting input and output in Windows you can find here.
Note that the <, > and >> symbols are to be entered verbatim — they are not just for presentation purposes in this explanation. So, for example:
App.exe < file1 >> file2
In addition to original redirection >/ >> and <
You can redirect std::cin and std::cout too.
Like following:
int main()
{
// Save original std::cin, std::cout
std::streambuf *coutbuf = std::cout.rdbuf();
std::streambuf *cinbuf = std::cin.rdbuf();
std::ofstream out("outfile.txt");
std::ifstream in("infile.txt");
//Read from infile.txt using std::cin
std::cin.rdbuf(in.rdbuf());
//Write to outfile.txt through std::cout
std::cout.rdbuf(out.rdbuf());
std::string test;
std::cin >> test; //from infile.txt
std::cout << test << " "; //to outfile.txt
//Restore back.
std::cin.rdbuf(cinbuf);
std::cout.rdbuf(coutbuf);
}
[I am just explaining the command line argument used in Question]
You can provide file name as command line input to the executible, but then you need to open them in your code.
Like
You have supplied two command line arguments namely inputfile & outputfile
[ App.exe inputfile outputfile ]
Now in your code
#include<iostream>
#include<fstream>
#include<string>
int main(int argc, char * argv[])
{
//argv[0] := A.exe
//argv[1] := inputFile
//argv[2] := outputFile
std::ifstream vInFile(argv[1],std::ios::in);
// notice I have given first command line argument as file name
std::ofstream vOutFile(argv[2],std::ios::out | std::ios::app);
// notice I have given second command line argument as file name
if (vInFile.is_open())
{
std::string line;
getline (vInFile,line); //Fixing it as per the comment made by MSalters
while ( vInFile.good() )
{
vOutFile << line << std::endl;
getline (vInFile,line);
}
vInFile.close();
vOutFile.close();
}
else std::cout << "Unable to open file";
return 0;
}
It is important that you understand the concept of redirection. Redirection reroutes standard input, standard output, and standard error.
The common redirection commands are:
> redirects standard output of a command to a file, overwriting previous content.
$ command > file
>> redirects standard output of a command to a file, appending new content to old content.
$ command >> file
< redirects standard input to a command.
$ command < file
| redirects standard output of a command to another command.
$ command | another_command
2> redirects standard error to a file.
$ command 2> file
$ command > out_file 2> error_file
2>&1 redirects stderr to the same file that stdout was redirected to.
$ command > file 2>&1
You can combine redirection:
# redirect input, output and error redirection
$ command < in_file > out_file 2> error_file
# redirect input and output
$ command < in_file > out_file
# redirect input and error
$ command < in_file 2> error_file
# redirect output and error
$ command > out_file 2> error_file
Even though, it is not part of your question, you can also use other commands are powerful when combined with redirection commands:
sort: sorts lines of text alphabetically.
uniq: filters duplicate, adjacent lines of text.
grep: searches for a text pattern and outputs it.
sed : searches for a text pattern, modifies it, and outputs it.
Related
I'd like to add every line to stdin, but I can't because a.out doesn't exit from loop.
C++ code:
#include <iostream>
int main() {
std::string in;
while(1){
std::cin >> in;
if(in == "exit")
break;
std::cout <<in << "\n";
}
return 0;
}
And a bash code:
#!/bin/bash
while read -r line
do
echo "$line" | ./a.out >> output.txt
done < "input.txt
Thanks!
your C++ part works perfectly. But as soon as it exits, you again do while read -r line. Try to enter EOF just after "exit" line.
I am wanting to use C++ to extract the contents of a .txt file (that will be pointed to from the terminal) that is to be parsed, but I am not sure how to go about doing this? I know that we do
./a.out < file.txt
which will then put file.txt into the standard input, but then I got confused because I am not sure how to access the file contents to parse it, etc.
You can treat the directed input as input into cin. Say if I had the following file input.txt
Hello
World
And you run your executable like
./a < input.txt
I could do something like
std::string s1;
std::string s2;
std::cin >> s1;
std::cin >> s2;
std::cout << s1 << s2 << "\n";
Which would then print Hello Word. getline with cin is also an option here. Think of it as though a very fast user were inputting a line immediately at each of your input statements.
You can get the filename as input on the arguments to main, and open an std::ifstream to that file.
int main(int argc, char* argv[]) {
std::ifstream file(std::string(argv[1]));
if (file.is_open()) {
//read input
}
}
You would call ./a filename. To read from file, you would do so as you do from std::cin (see NickLamp's answer).
Currently I have code that uses cin to get the name of the file to be used as input when the program is executed. I would like to have it so that when i run the program i can add the file redirection and the filename and run it as such: ./a.out < file.txt. How can i feed my file into my code using redirection.
this is an example of how i am currently taking input:
int main(){
std::string filename;
std::cin >> filename;
std::ifstream f(filename.c_str());
}
Do it like this
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string line;
std::cout << "received " << std::endl;
while(std::getline(std::cin, line))
{
std::cout << line << std::endl;
}
std::cout << "on stdin" << std::endl;
}
You do not need to open the file yourself as the file content is passed to you on stdin as a result of using < on the command line.
This code has a drawback that if you do not input anything on stdin it freezes. Detecting that stdin is empty can only be achieved in a non portable manner (see here).
It would be better to accept your filename as a normal command line argument and open it inside your program.
I want the user to give the file name in output screen,for the file to be opened while running the program .More specifically, if the user gives a file name in output screen, my program should open that file for him. How can i code this using files in c++? Can anyone give an example?
example code:
#include <iostream>
#include <fstream>
int main() {
std::string filename, line;
std::cout << "Input file name: ";
std::cin >> filename;
std::ifstream infile(filename);
if(!infile)
std::cerr << "No such file!" << std::endl;
else {
std::cout << "File contents: " << std::endl;
while(infile >> line)
std::cout << line << std::endl;
}
}
contents of test.txt:
hello
world
how
are
you
program output (on console):
Input file name: test.txt
File contents:
hello
world
how
are
you
the way this works is, you declare two strings - one that will be the filename and the other that will be serve as a temporary buffer for each line in file.
when the user inputs a file name, depending on whether the file exists or not, the program will either output an error, or declare and initialize a file stream infile (this basically opens a stream to your file and allows you to extract [or input] data from the desired file).
once that is done, the program will read the file line by line while(infile >> line) and output the contents of each file to console.
I've got a .cpp which is prompted as follows:
$ ./program file < file.txt
Then I want to use the text on the file.txt for some functions inside my program.
How can I access the input on the .txt on my .cpp?
stdin?
cin?
could you put some examples?
You must use std::cin
#include <iostream>
#include <string>
int main() {
for (std::string line; std::getline(std::cin, line);) {
std::cout << line << std::endl;
}
return 0;
}
you can use ifstream to open your file and getline function to read it line by line. You don't need to use < to pass param to your program. The param can be get in the argv array of your main function