User Defined save file (help)

This is a discussion on User Defined save file (help) within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program with a save option, but let the user choose the name of the ...

  1. #1
    Unregistered
    Guest

    Question User Defined save file (help)

    I am trying to make a program with a save option, but let the user choose the name of the file.

    eg:


    char UserDefName[30];
    ofstream SaveGAME(UserDefName + ".txt");



    But that doesn't work, so I need a way to add '.txt' to the end of the UserDefName string, any suggestions?

    Paul

  2. #2
    Unregistered
    Guest
    strcat

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This would be a wonderful time to use the string class. Simplicity in programming is heaven.
    Code:
    string UserDefName;
    .
    .
    ofstream SaveGAME(UserDefName + ".txt");
    But if you must use C strings, place the ".txt" extension onto the end of the string with strcat in the <cstring> header.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    Okay, this is what i did, but it didn't seem to work, I must have the wording wrong.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char UserName[20];
    char Extension[5] = ".txt";
    
    
    int main()
        {
    
    
        cout << "Type the name of the game to be saved: ";
        cin >> UserName;
    
        strcat(UserName & ".txt");
    
        ofstream SaveGAME(UserName);
                 SaveGAME << "Hello";
                 SaveGAME.close();
    
        }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    char UserName[20];
    char *Extension = ".txt";
    
    int main()
        {
        cout << "Type the name of the game to be saved: ";
        cin >> UserName;
    
        strcat(UserName, Extension);
    
        ofstream SaveGAME(UserName);
        SaveGAME << "Hello";
        SaveGAME.close();
        }
    Be sure to check that there is room in UserName for the extra characters.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Extension is gonna have be char Extension[7] I think thus

    " . t x t " /0

    I have no idea where you got the '&' in function strcat from.

    You cannot input a char array like that: you have to use string class or else a loop.

    Main() returns 0.

    BTW, thanks for all the descriptions of errors that you got compiling...
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  7. #7
    Unregistered
    Guest
    Oh thanks you guys are legends. How do you know all this stuff? Is there like a sacred forest of header files that you can go to and everything is explained there?

    All is good now. Thanks.

    Paul

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Extension is gonna have be char Extension[7] I think thus
    Where do you get seven? The double quote characters delimit a string unless the escape character \" is used. That makes 5, but strcat takes the first character of the source string and uses it to replace the nul terminator of the destination string, so you really only have to consider 4.

    >I have no idea where you got the '&' in function strcat from.
    I imagine he thought that some sort of bitwise operation took place, either that or a typo.

    >You cannot input a char array like that: you have to use string class or else a loop.
    Huh? I assume you mean cin>>UserName, which is perfectly legal and will work fine unless the user enters a name with spaces in it.

    >Main() returns 0.
    Or some variation of success or failure. There are three portable values that it returns, 0 being one of them.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there like a sacred forest of header files that you can go to
    >and everything is explained there?
    Yes, it's called JTC1/SC22/WG21.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    >>Is there like a sacred forest of header files that you can go to
    >>and everything is explained there?
    >Yes, it's called JTC1/SC22/WG21.

    Lol!!

    Well actually strcat() is just part of a little string function family that almost everyone uses sometime.
    Theres also strcmp() for comparing strings, strcpy for copying strings and strtok() for tokenizing strings.

    And then there are som others that i dont know of yet....hmm....*goes to visit JTC1/SC22/WG21*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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