Solved

file redirection

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 109
Which Linux flavors will this run on? 6 103
gdb doesn't stop on breakpoint 2 114
Need some help with design 17 23
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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