Thread: save file as

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    save file as

    im having a simple problem in C. i am trying to prompt the user to enter a file name to save as. the only problem is i only know how to open an existing file and write onto that. i want to be able to take in info and save it as whatever the user wants it to be called. the same thing with loading. how do i do this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by vidioholic View Post
    the only problem is i only know how to open an existing file and write onto that. i want to be able to take in info and save it as whatever the user wants it to be called. the same thing with loading. how do i do this?
    You say that as though there is a difference. There is not.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    im doing it like this:

    printf("Enter a file name to save as:\t");
    scanf("%s", &newfile);
    fout = fopen("filename.txt" ,"w");
    fclose (fout);

    the problem is i dont know how to get the new file name to replace the filename i already have or to just create a new file all together named after whatever the user chooses.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And why didn't you even try fopen(newfile, "w")?

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i think i figured it out:

    Code:
        
    printf("Enter a file name to save as:\t");
        scanf("%s", &filename);
        fout = fopen(filename, "w");
        fclose (fout);
    this is the way to do it right?

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    oh lol well now i tried it. the problem was i thought there had to be something in quotes so i was even tring
    Code:
     fopen("%s", filename,"w");
    but obviously that was wrong. im still new at this whole thing as you can tell. thanx for your help though. ill post again some time soon most likely

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    scanf("%s", &filename);
    if filename is char-array - you do not need the & here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save a file with a name in a variable?
    By guriwashere in forum C Programming
    Replies: 2
    Last Post: 06-01-2009, 04:03 PM
  2. 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
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM