simple C++ question

Bluga

Banned
Nov 28, 2000
4,315
0
0

If i have a file called "1.input", and i use fopen() function to open the file and proccess it, now i need to print its output to a file named "1.output", how should i do that? Thanks.
 

Juniper

Platinum Member
Nov 7, 2001
2,025
1
0
Take a look at this pdf file: link.

From page 16 onwads, its about pipes. Its not bad, the lecturer even gives some snippet of codes. Hope this helps. I remember doing this, spent days on a program.
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
You should use

int main()
{
FILE *f;
f = fopen("1.input", "rt"); // in mode read only, text if(f == NULL)
{
printf("Non existing file \n");
return 0;
}
char myChar;
fscanf(f, "%c", &myChar); // read from file f one char and set its value in variable myChar
fclose(f); // close the IN file
FILE *fOut;
fOut = fopen("1.output", "wt"); // open file 1.output in write mode. All content will be erased
// if you want the content not erased, use "at" instead of "wt" (append text mode not write text mode"
if(fOut == NULL)
{
printf("Error opening output file\n");
return 0;
}
fprintf(fOut, "%c", myChar);
fclose(fOut);
return 1;
}

If you use a real C compiler, you should move all the var declaration at the beggining of the block
(immediatly after "main(){" ).

The codes for var types are:
%c - char
%d - integer
%x - hexadecimal
%f - float %s - character string

Calin