Solved

file redirection

Posted on 2000-03-24
5
206 Views
Last Modified: 2010-04-02
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
Comment
Question by:kkooty
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2653724
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
 
LVL 22

Expert Comment

by:nietod
ID: 2653746
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
 

Author Comment

by:kkooty
ID: 2653833
thanks that was usefull.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2653969
If you are "done" then you should grade the question.  If not, no rush.
0
 

Expert Comment

by:maxd128
ID: 2655526
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now