ONLINE_JUDGE with C++

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

Moderator: Board moderators

Post Reply
rbuchan
New poster
Posts: 27
Joined: Fri Feb 28, 2003 7:59 am
Contact:

ONLINE_JUDGE with C++

Post by rbuchan »

I have figured out how to do this using stdio.h, but I am not sure how to do this with iostream/fstream. Here is the example code that is working:

[cpp]
#include <stdio.h>

#ifndef ONLINE_JUDGE
FILE *fin = fopen("myprog.in", "r");
FILE *fout = fopen("myprog.out", "w");
#else
FILE *fin = stdin;
FILE *fout = stdout;
#endif

int main(){
unsigned int n,i,j,max=0;
long temp;

while (fscanf(fin,"%d",&i)==1){
...
if (ch==0)fprintf(fout,"%d %d %d\n",i,j,max);
else fprintf(fout,"%d %d %d\n",j,i,max);
}
return 0;
}
[/cpp]

I need to have the users to be able to do this with the C++ style of coding rather than C. I want them to have the option to do both. There was a posting up here before, but I can't find it.

Thanks
Cheers,
Ron

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

I think you can still do it using C++, you do not need any other code except just converting fopen to cin.open() fn and printf to cout.open() fn.

Anupam
"Everything should be made simple, but not always simpler"

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post by Per »

Believe it or not, there is actually a reason why there is a C++ forum.
http://online-judge.uva.es/board/viewforum.php?f=14

You can do like this, and use cin/cout as usual:
[cpp]#include<fstream>
#include<iostream>

void init() {
#ifndef ONLINE_JUDGE
cin.rdbuf((new ifstream("T.in"))->rdbuf());
cout.rdbuf((new ofstream("T.out"))->rdbuf());
#endif
}[/cpp]
Or, you could have two global i/ofstream variables which are just the same as cin/cout if ONLINE_JUDGE is defined, similarly to what you did in the C case.

Post Reply

Return to “C++”