-
Input/Output
I am having a problem with input/output. I have created a text file called input.txt and want to move this to output.txt
I have been able to move a single character over with %c but when I have tried to use %s it doesn't appear to work.
This is the code I have used:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main (void)
{
char e[30];
char temp_char;
int counter;
FILE *f_input;
FILE *f_output;
for(counter=0; counter<200; counter++)
if ((f_input = fopen("input.txt","r"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
fscanf(f_input,"%c", &temp_char[counter]);
if ((f_output = fopen("output.txt","w"))==NULL)
{
printf("Cannot open file for writing\n");
exit(1);
}
fprintf(f_output,"The input file contained the following: %c \n",temp_char);
fclose(f_input);
fclose(f_output);
getch();
}
Can anyone help, thanks if you can.
-
try this
Code:
int main()
{
int ch;
FILE *f_input, *f_output;
if ((f_input = fopen("input.txt","r"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
if ((f_output = fopen("output.txt","w"))==NULL)
{
printf("Cannot open file for writing\n");
exit(1);
}
while((ch = fgetc(f_input)) != EOF)
fputc(ch, f_output);
fclose(f_input);
fclose(f_output);
return 0;
}
look up fgetc, fputc and EOF in your documentation
-
Thanks for that - It is now working.
Out of interest, if I wished to transfer message over but this time backwards will I be ok to use a'For' loop?
-
Of course.
You just read the message from the end to the start and print it out on the screen.