Redirecting standard IO

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
BiK
Experienced poster
Posts: 104
Joined: Tue Sep 23, 2003 5:49 pm

Redirecting standard IO

Post by BiK »

Could you tell me how to use the ONLINE_JUDGE symbol in C++ in order to test my program using file input and file output. In other words I need the C++ equivallent code of the following C code:

[c]

#ifndef ONLINE_JUDGE
close (0); open ("myprog.in", O_RDONLY);
close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif
[/c]
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

you can use the freopen() function to test your programs.
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

I'm usually running my programs from command-line giving file as input stream:

Code: Select all

progname <inputfile
It works in win/dos, if I'm correct it will work on linux too.
Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

It works with Linux as well. It's a surprise - I always used

Code: Select all

cat input|progname
but your form is shorter, so I'm going to switch :-)
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

i discovered that a couple of days ago. I needed to dump the program output to a file, and as I'm not a linux user I just tried '>filename'... it worked, so I supposed '<filename' would also work. good to hear it works ;)
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
BiK
Experienced poster
Posts: 104
Joined: Tue Sep 23, 2003 5:49 pm

Post by BiK »

Hi Subeen. You mean to replace the open function with freopen or what? Could you type the actual C++ code. I know nothing about freopen. And I want to run my programs under Windows.
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

here is it:
for reading from file:

Code: Select all

freopen("input file name", "rt", stdin);
and for writing in file:

Code: Select all

freopen("output file name", "wt", stdout);
just add these two lines before ur program ( in main method )
Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post by Per »

That would still be the C way of doing it. If you're using C++ iostreams, you can write like this (though there may be a simpler way?):

Code: Select all

#include <fstream>

cin.rdbuf((new ifstream("___.in"))->rdbuf());
cout.rdbuf((new ofstream("___.out"))->rdbuf());
(And as long as it's for programming contest purposes, there's no reason to worry about freeing the created streams in the end.)

But I say I definitely recommend doing it Ivors way, redirecting the I/O from/to file with "<" and ">". That way you don't have to clutter up your code more than necessary.
BiK
Experienced poster
Posts: 104
Joined: Tue Sep 23, 2003 5:49 pm

Post by BiK »

Thanks guys. Now everything is OK.
macin
New poster
Posts: 6
Joined: Tue Apr 06, 2004 6:40 pm

Post by macin »

my flavor:

Code: Select all

std::filebuf in, out;
std::cin.rdbuf(in.open("stdin.txt", std::ios_base::in));
std::cout.rdbuf(out.open("stdout.txt", std::ios_base::out));
Post Reply

Return to “C++”