Thread: Creating backups...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Creating backups...

    Would there be a way to open a file and the program wil ask u if u want to save it as a bak file? Would it be something similiar to fopen? for it opens it to read then saves as a file u designate it as? Or open winrar and create a rar file with fopen/ something else?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sure, you (not u) do

    remove("file.bak");
    rename("file.txt","file.bak");
    fp = fopen( "file.txt", "w" );

    Then do what you would normally do to write the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Ok so if wanted to copy the whole file and save it in a arrary to write to another file somewhere else like a backup folder? would that work i got this code so far

    Code:
    #include <stdio.h>
    
    int main()
    {
        char account[50];
        FILE *Bakfile;
        FILE *fp;
        
        printf( "Please Choose from list below of which file to make a backup of\n" );
        fgets( account, 50, stdin);
        rename("account.txt","account.bak");
        fp=fopen("c:\\backups\account.txt", "r");
        fprintf(fp, "%s", account );
        system("pause");
        return 0;
    }

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Why are you trying to open account.txt when you just renamed it to account.bak? Also, don't you mean "c:\\backups\\account.bak", with two backslashes?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Quote Originally Posted by Rashakil Fol
    Why are you trying to open account.txt when you just renamed it to account.bak? Also, don't you mean "c:\\backups\\account.bak", with two backslashes?
    I forgot to fix it up after i posted i found those errors after i posted sorry ^_^

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Even in DOS/Windows, you can specify paths with slashes:
    Code:
    "c:/backups/account.bak"
    This has the advantage of not needing to escape backslashes (so less error prone), and being more consistent across platforms.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Error in creating socket in a client (UDP)
    By ferenczi in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-27-2008, 11:11 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  5. Creating Custom Controls
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 12-29-2003, 01:41 PM