• Log in or sign up for an account here: https://www.sythe.org/login

Quick help needed

mopar-user

Guru

Banned
Joined
Jul 23, 2007
Sythe Gold
0
Messages
1,188
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?
 
You mean you want to create a file using the name they input?
 
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 ^^
 
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;
}
 
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;
}
 
Runescape Gamblingrunescape gambling
Back
Top