Basic read and write with files
May 25, 2012 at 9:09am UTC
This code I have made, is supposed to open a file (source) and copy its text to another one (copy). But what it really does is not creating the "copy" file and erasing all the content from the "source" file.
Any help with this?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE *source, *copy;
char name[100];
char c;
printf("Name of the source file: \n" );
scanf("%99s" ,name);
if ((source=fopen(name,"r" ))==NULL)
{
printf("We didn't find the file %s\n" ,name);
}
else
{
printf("Name of the copy file: \n" );
scanf("%99" ,name);
if ((copy=fopen(name,"w+" ))==NULL)
{
printf("We couldn't create file %s\n" ,name);
}
else
{
while ((c=getc(source))!=EOF)
{
putc(c,copy);
}
}
}
fclose (source);
fclose (copy);
system("PAUSE" );
return 0;
}
May 25, 2012 at 9:20am UTC
in line 21
scanf("%99s " ,name);
;o)
Last edited on May 25, 2012 at 9:21am UTC
May 25, 2012 at 9:23am UTC
Oh my God!
What a silly mistake! I was going crazy reviewing the code over and over again!
Thanks shadow123!
May 25, 2012 at 9:33am UTC
Topic archived. No new replies allowed.