![]() |
|
#1
|
|||
|
|||
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:
But then again, I'm not sure. Any clarification on this subject would be a great help, thanks! vital_101 |
|||
|
#2
|
|||
|
|||
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
|
||||
|
||||
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
|
|||
|
|||
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:
The syntax I have been given for my "input redirection" is a.out < inputfile.dat > outputfile.dat |
|
#5
|
||||
|
||||
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:
Quote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#6
|
|||
|
|||
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
|
||||
|
||||
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 GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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