GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jan-2006, 12:17
vital_101 vital_101 is offline
New Member
 
Join Date: Jan 2006
Posts: 9
vital_101 is on a distinguished road

input / output redirection in C++


I have recently been introduced to the concept of input/output redirection using the shell. My problem is that I'm not sure how to code to allow it to happen. For instance, I know how to write a program that opens a file, reads from it, and closes it. However, I'm not sure how to write something that would read from the file specified in the statement: a.out < prog1in.dat > prog1out.dat
I know that the program should read information from prog1in.dat, and put it in prog1out.dat, but I don't know how to write that.

I assume it should be something like
CPP / C++ / C Code:
cin >> something;

But then again, I'm not sure. Any clarification on this subject would be a great help, thanks!

vital_101
  #2  
Old 18-Jan-2006, 14:05
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: input / output redirection in C++


when your program has its input/output redirected by the shell, no cooperation from you is required. cout will be replaced by prog1out.dat and cin by prog1in.dat (as opposed to the usual screen console "file"). so just use the standard cin/cout.
  #3  
Old 18-Jan-2006, 18:53
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: input / output redirection in C++


cin reads from stdin and cout writes to stdout. On the command line, < redirects stdin to read from the file specified, and > redirects stdout to create and write to the file specified. >> appends to the file.

This tutorial describes how redirection works. Just remember, printf() and cout are synonymous.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 19-Jan-2006, 17:10
vital_101 vital_101 is offline
New Member
 
Join Date: Jan 2006
Posts: 9
vital_101 is on a distinguished road

Re: input / output redirection in C++


Okay, I understand the basics of input and output redirection. I now have a question that's a little more tricky. Lets say I want to read input from a file AND from the keyboard. And in the same program, I want to output to the screen AND to a file.

Ex:
Quote:
cout << "Please enter your number" << endl;
cin >> number;
//Then, the program outputs this number to the output file.
cout << number;
//Then, grabs a number from an input file
cin >> number;

The syntax I have been given for my "input redirection" is a.out < inputfile.dat > outputfile.dat
  #5  
Old 19-Jan-2006, 21:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: input / output redirection in C++


Then write your input read the file when you want a file, the keyboard when you want a keyboard. Redirection is not meant to be a complex (tricky) idea. You either redirect or you don't. Mixing you must design into your program I/O directly.

The best you can hope for on the output side using redirection is output that you might want to go to a file via redirection use cout. Output that always goes to the screen use cerr.

As for input, redirect or not only.

As for
Quote:
Originally Posted by vital_101
The syntax I have been given for my "input redirection" is a.out < inputfile.dat > outputfile.dat
that is how it works. Although I personally remove the spaces:
Quote:
a.out <inputfile.dat >outputfile.dat
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 19-Jan-2006, 22:01
vital_101 vital_101 is offline
New Member
 
Join Date: Jan 2006
Posts: 9
vital_101 is on a distinguished road

Re: input / output redirection in C++


Thanks for the help WaltP. I figured it wasn't supposed to be difficult. Apparently my professor enjoys making things much more difficult than they are.
  #7  
Old 19-Jan-2006, 22:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: input / output redirection in C++


I think you probably misunderstood his instructions. I doubt he tried to make it difficult. He might have made it unclear though.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] Discarding input Alhazred C Programming Language 8 04-Aug-2004 11:45
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16
IP tables rogermark100 C Programming Language 6 18-Apr-2004 07:22
using vector or array oshiotse C++ Forum 4 16-Apr-2004 10:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:28.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.