23299

Passing text file to standard input

Question:

The following code is part of a larger translator program. The code below asks the user to type a line and than just writes it back. Is there a way that instead of writing a single line each time, I can just pass in a whole file etc 'translate.txt' in standard input and the program can write it back line by line and produces an error when the end of line is reached ?

#include <iostream> #include <string.h> #include<stdio.h> #include<fstream> using namespace std; using namespace std; void PL() { char line[BUFSIZ]; while( cin.good() ) { cout<<"Type line now"<<endl; cout<<"\n"; cin.getline(line, sizeof(line)); cout<<"\n"<<endl; string mystring = string(line); // supposed to Parsing string into words and translate// //but just reading back input for now// cout<<"You typed:"<<mystring<<endl; cout<<"\n"<<endl; } } int main() { PL(); }

Answer1:

Do you expect a way to pass a file to your program?

executable < file

Answer2:

<a href="http://ideone.com/XN91DP" rel="nofollow">This code works well for me</a>:

void PL() { string line; while(cin) { cout<<"Type line now"; if(std::getline(cin,line)) { // supposed to Parsing string into words and translate// //but just reading back input for now// cout<<"You typed:"<<line<<endl; } } }

Note that the stdin there is actually passed to the program from the shell as mentioned:

$ executable < file <hr />

If you want to pass arbitrary types of streams created from outside this function, you'll need something like

void PL(std::istream& is) { string line; while(is) { cout<<"Type line now"; if(std::getline(is,line)) { // supposed to Parsing string into words and translate// //but just reading back input for now// cout<<"You typed:"<<line<<endl; } } } int main() { std::ifstream is("mytext.txt"); // hardcoded filename PL(is); return 0; }

or alternatively

int main(int argc, char* argv[]) { std::istream* input = &std::cin; // input is stdin by default if(argc > 1) { // A file name was give as argument, // choose the file to read from input = new std::ifstream(argv[1]); } PL(*input); if(argc > 1) { // Clean up the allocated input instance delete input; } return 0; }

<sub>There are certainly more elegant solutions</sub>

and calling from the command line:

$ executable mytext.txt

Answer3:

Your shell will have a way to pass a file in over stdin. So for example, if you are on a bourne-compatible shell you can run

translate < translate.txt

(assuming your program is compiled to a binary named translate). This is assuming you want to start the program interactively, i.e. from a shell.

If you want to automatically spawn this program from another program you wrote, it depends on your OS. On POSIX operating systems, for example, you will want to open the file and dup2 the resulting file descriptor into STDIN_FILENO after forking but before calling one of the exec family functions.

Recommend

  • fstream get(char*, int) how to operate empty line?
  • CLion prints the input back
  • How to allow user to do input more than twice?
  • How do I add data at the end of a file in C++?
  • Dividing the length by sin(x) always results in a negative answer
  • What's the equivalent for while (cin >> var) in python?
  • C++ Copy directory recursive under unix
  • Junk characters while reading text file in java
  • How can I keep open the command prompt in my windows form application in c++?
  • C++ Boost ptr_map serialization error
  • Need help to stop program terminating without users consent
  • Smiles in output C++
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • Iterating over a container bidirectionally
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • How to work with AMMediaType for video filters
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • OpenMP and File I/O
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • c++ regex_replace not doing intended substitution
  • How to unpack 32bit integer packed in a QByteArray?
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • WPF Template Binding in ToggleButton UserControl
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • CERN ROOT exporting data to plain text
  • Reduction and collapse clauses in OMP have some confusing points
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Why is the size of this struct 32?
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • Do create extension work in single-user mode in postgres?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs