Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

file redirection

how do we  read in  a data file using redirection sign ie < so that we  are able to pass the data in and use in the program. we want to be able to type

a.out < filename  where a.out  is the executable and filename is the text file
with data in it.
0
kkooty
Asked:
kkooty
  • 3
1 Solution
 
nietodCommented:
When you redirect a file to a program, the file becomes "standard input" for the program.  "Standard input" is one of the three standard files associate with a C/C++ program.  (The others being "standard out" and "standard error".)

You can extract information from standard input in many ways.  the typical way for  a c++ program is to read it from the standard input stream object called "cin".  

continues.
0
 
nietodCommented:
If you are not familar with cin and/or stream objects, you need to include the file <iostream> which defines the input stream class "istream" and the "cin" object of this class.  both of these names are defined in the "std" namespace, so you may wisht to include a "using namespace std;" line otherwise you will have to specify "std::" before these names.

You can read text from the input stream object using the getline() member function, but this is inconvenient if you don't know the length of the line to be read (you have to set an arbitaray limit.)  so I prefer to use the global getline function which reads a line of any length into a string object, like

string CurLin

getline(cin,CurLin); // read a line.

let me know if you have any questions.
0
 
kkootyAuthor Commented:
thanks that was usefull.
0
 
nietodCommented:
If you are "done" then you should grade the question.  If not, no rush.
0
 
maxd128Commented:
Executing command
type filename | a.out
will pass text found in file "filename" to the standard input stream (like scanf or cin) of executable a.out
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now