Quick help needed

Discussion in 'Programming General' started by mopar-user, Oct 25, 2009.

Quick help needed
  1. Unread #1 - Oct 25, 2009 at 12:11 PM
  2. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    Quick help needed

    Ok, I'm making a game in C++. I'm using Dev C++ compiler. My issue deals with opening and close files.


    heres an example of what i need...


    I ask the user their name.
    They input the name.
    The name is assigned to a variable.

    Now the hard part for me...

    loadSave.open(This is where i want the variable for their name to be)

    can't get it to work. I know it has something to do with .c_str() but i cant get it to work for the life of me. Help me please?
     
  3. Unread #2 - Oct 29, 2009 at 9:57 AM
  4. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    Quick help needed

    You mean you want to create a file using the name they input?
     
  5. Unread #3 - Oct 29, 2009 at 3:41 PM
  6. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    Quick help needed

    Yes. Much easier said that way lol.
     
  7. Unread #4 - Oct 30, 2009 at 3:11 PM
  8. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    Quick help needed

    I don't really know the syntax in c++
    but if you are just going to use their name as the file name.

    Declare a variable for the file name and not the FILE creation variable.
    Declare another variable that will hold their name.

    In example...

    string filename;
    string name;
    string ext=".txt"; // in example that you are going to make a .txt file

    Ask for user name input

    filename = name + ext; // I'm not sure if this is the correct format. I made a little searching and this is what I get (concatonate two strings in c++. but in c i know it is strcat(string1, string2); ) and you need the syntax for concat on two strings.

    use the open file (with the file name as the variable filename)

    and there you have it.
    I hope I made my explanation understandable a little.
    If not, why don't you put your code so maybe I can help you a little better.
    Happy programming ^^
     
  9. Unread #5 - Jan 13, 2010 at 12:27 PM
  10. Eduard
    Joined:
    May 26, 2006
    Posts:
    45
    Referrals:
    1
    Sythe Gold:
    0

    Eduard Member
    Banned

    Quick help needed

    i'll write something for you. if you want a name for input. you use scanf();
    then you'll need to declare an FILE pointer. and write a buffer from the input into the FILE.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char input[10]; // input buffer with a capicity of 9 character + 0 terminater.
        FILE *fp; // FILE datatype and a file pointer (FP).
        
        printf("Username please: ");
        scanf("%s",&input); // pointer to input. to store the input from the stdio
        printf("\n Your input is %s ",input);
        fp = fopen("C:\\username.txt","w+");
        fprintf(fp,input);
        fclose(fp);
            return 0;
    }
    
     
  11. Unread #6 - Jan 13, 2010 at 12:34 PM
  12. Eduard
    Joined:
    May 26, 2006
    Posts:
    45
    Referrals:
    1
    Sythe Gold:
    0

    Eduard Member
    Banned

    Quick help needed

    i'll write something for you. if you want a name for input. you use scanf();
    then you'll need to declare an FILE pointer. and write a buffer from the input into the FILE.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char input[10]; // input buffer with a capicity of 9 character + 0 terminater.
        FILE *fp; // FILE datatype and a file pointer (FP).
        
        printf("Username please: ");
        scanf("%s",&input); // pointer to input. to store the input from the stdio
        printf("\n Your input is %s ",input);
        fp = fopen("C:\\username.txt","w+");
        fprintf(fp,input);
        fclose(fp);
            return 0;
    }
    
     
< Compiler problem | [Aimbot]Arcanists[Possible payment?] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site