copying a file

This is a discussion on copying a file within the C Programming forums, part of the General Programming Boards category; anyone know how to copy a file? don't get it. beginner....

  1. #1
    Unregistered
    Guest

    copying a file

    anyone know how to copy a file?
    don't get it.
    beginner.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	system("copy filename.txt newfile.txt");
    
    	return 0;
    }
    Works in MSVC++6 or VS.NET beta 2.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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;
    }

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Fordy, when you say system, do you mean compiler?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    >>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

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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?

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    >>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

  8. #8
    Unregistered
    Guest
    thanks!

    what about if you need the user to input the file they want copying?
    beginner (slow beginner!)

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	system("cmd");
    	printf("You typed exit, goodbye!\n");
    
    	return 0;
    }
    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.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21