save as file name?

This is a discussion on save as file name? within the C++ Programming forums, part of the General Programming Boards category; Hi... i would like to know how one can save the filename according to what the user types... cin>>whatusertypes; write.open("whatusertypes.txt",ios: ...

  1. #1
    Adrive
    Guest

    save as file name?

    Hi...
    i would like to know how one can save the filename according to what the user types...


    cin>>whatusertypes;
    write.open("whatusertypes.txt",ios:ut);

    that doesn't seem to work...it'll produce whatusertypes.txt instead of what the user really typed....any ideas??

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    ---
    ---
    char fname[12];

    cout<<"enter file name : ";
    gets(fname);

    fout.open(fname,ios:ut);
    ---
    ---

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Well duh. You can't put quotes around a string name, then it'll parse it as an actual string...

  4. #4
    Adrive
    Guest
    Thanks! it worked fine but how can i let the computer add the extension for the filename entered by the user??

    such as *.txt type

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    #include<fstream>
    #include<string.h>

    fstream write;
    char whateverusertypes[10];

    cin >> whatusertypes;
    write.open(whatusertypes ,ios:ut);
    strcat(whateverusertypes,".txt");

    ------
    strcat() will append the second parameter (a string) to the first (another string). It requires the header string.h

    The Gekko

  6. #6
    Adrive
    Guest

    Unhappy

    i'm sorry....but the code you gave couldn't work....

    it would still save the file name as what the user typed, but without the extension

    is there a clue??

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try it in this order -

    cin >> whatusertypes;
    strcat(whateverusertypes,".txt");
    write.open(whatusertypes ,ios::out);

    Make sure whatusertypes has enough memory allocated for it's description + 4.

  8. #8
    adrive
    Guest

    Cool

    thanks!! it worked like a charm!

    does anyone know how we can make the program read the harddisk

    if the required file is not found, then ask the user to create?

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Hahaha...whoops...sorry about that man. Thats what i get for cut and pasting your code instead of just rewritting my own

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Im not sure what you want. With what your doing the file is created no matter what. If its there its replaced..if not its created. Do you want to ask to replace it if theres already one there instead of just overwritting it?

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    And one more thing...start looking through code you get to understand it instead of just throughing it in your program. It will help you learn a lot. That bug in my program should have been real easy for you to spot if you had looked at it. It makes me look bad, it being so simple to spot

  12. #12
    adrive
    Guest

    Smile

    thanks for the advice
    you see i'm a student and i'm doing a project
    the thing is, what i've learned in school just isn't enough

    thats why i don't know alot of stuffs..

    hm about the file thingy, what i wanted is, the program would check the harddisk (well local folder only anyways) and if the file is found.......which is, a year's record, then it'll carry on

    if that file is not found, that it prompts the user to create a new record..so is there a way to search for the file's presence?

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Hmmm...there prob a better way...but you could try to open it and test for an error...if theres no error opning the file you could prob assum it was there and if there was an error than it wasn't there. Not perfect I know, but if noone else speaks up it will prob work for you.

    right after you open the file do this,

    if (!fileName)
    {
    error opening...do whatever
    }
    else
    {
    found it...do whatever
    }

    I cant remeber if your going to have to use ios::nocreate or not when opening it...

  14. #14
    adrive
    Guest

    Post

    I'm sorry, but i've tried thousands of ways to make the below correct but it just can't work!

    int data=add[hold].staff_id;
    strcat(c:\windows\desktop\data,".txt");
    write.open(data,ios:ut);

    does this means strcat can only work for characters? my structure staff id is an integer, therefore it can't combine it with the "txt extension. Is there a way to solve this??

    btw...the c:\windows bla bla . Do we use "\" or "/" ???

  15. #15
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Why don't you input the extension while entering the name of file.

    like

    gets(fname); //myfile.dat



    from your code above it looks like you want to save the file in another directory.
    use the chdir command in dir.h

    chdir("c:\windows\dektop");

    --- do the file stuff ---

    chdir("???");

    here ??? implies the c++ directory, like mine is "c:\langs\tc\bin".

    check your help on dir.h for the commands relating to directory.

Page 1 of 2 12 LastLast
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, 05: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

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