Solved

file redirection

Posted on 2000-03-24
5
201 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

758 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

19 Experts available now in Live!

Get 1:1 Help Now