Solved

file redirection

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

929 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

14 Experts available now in Live!

Get 1:1 Help Now