anyone know how to copy a file?
don't get it.
beginner.
anyone know how to copy a file?
don't get it.
beginner.
Works in MSVC++6 or VS.NET beta 2.Code:#include <stdio.h> #include <stdlib.h> int main() { system("copy filename.txt newfile.txt"); return 0; }
Its system dependant....
For Windows you could use CopyFile() like so;
Code:#include <windows.h> int main(int argc, char *argv[]) { if(CopyFile("oldfile.txt","newfile.txt",FALSE)){ MessageBox(0,"File Copied AOK :)","",MB_OK); } else MessageBox(0,"Doh!!! Copy Failed :(","",MB_OK); return 0; }
>>Fordy, when you say system, do you mean compiler?
No...sorry I didnt explain myself.....
I mean I think the file copy methods are system dependant.......
The method I gave is definately system dependant.....also as you point out....that needs a windows compiler like DevC++ or MSVC++ or Borland Builder
The system command uses the command shell. It should work on all versions of Windows. But ofcourse it does not work on *nix, but who cares!
I like it because it is so simple to use. But in reality if it were my program I would use C# and the BCL. Not sure how to use File/IO yet with C#. But when I do, I'll be system independant because I'll target the .NET framework.
I think that most of the questions here on the C board are from students learning the C/C++ ANSI definitions. Although that might not always be the case. 'system' requires the Microsoft compiler, it is far easier to use than Win32. Look at the differences in the code!
BTW did you order the new Petzold yet?
>>The system command uses the command shell. It should work on all versions of Windows. But ofcourse it does not work on *nix, but who cares!
I dont personally like using system().......i use it now and again, but I would normally prefer another method.....but then its a question of personal choice......
>>BTW did you order the new Petzold yet?
Nah....I'm reading Prosise's MFC Programming at the moment......It'll be a while until I get round to looking at C#.........not enough time in the day
thanks!
what about if you need the user to input the file they want copying?
beginner (slow beginner!)
Sure you could do it the way the previous poster suggested or else you can just open the shell and do whatever editing you want. It depends on the context of the program. The code the other poster suggested above asks for the name of the file and checks if that file exists by trying to open it. If it doesn't open than it doesn't exist. If it opens than you copy the contents of the file into the destination file with a buffer.Code:#include <stdio.h> #include <stdlib.h> int main() { system("cmd"); printf("You typed exit, goodbye!\n"); return 0; }
Just another example. It might be easier to read.
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> void CopyFile(); void GetFileName(char []); int main() { CopyFile(); return 0; } void GetFileName(char filename[]) { fgets(filename,256,stdin); filename[ strlen(filename) -1 ] = '\0'; } void CopyFile() { char source[256]; char dest[256]; int character; printf("Enter source filepath: "); GetFileName(source); FILE *fsource = fopen(source,"r"); if(fsource == NULL) { printf("Source File Does Not Exist!"); exit(1); } printf("Enter destination filepath: "); GetFileName(dest); FILE *fdest = fopen(dest,"w"); while( fscanf(fsource,"%c", &character) != EOF) { fprintf(fdest, "%c", character); } printf("File copied sucessfully"); }