Hi Again,

Well i'm bored and i've decided to do some coding just for practice and i've decided to work on a text based RPG, its only going to be basic due to my own knowledge but i think its doable. unfortunatly i've come accros a problem already and i've only writen a few lines of code.

The code deals with creating new character data, i plan to have all the character data stored in a text file, when the user starts a new game the program will ask for the character name then create a text file with this name write the players name and then close the text file.

heres the code that i have so far, this will eventualy be put into its own seperate function but for now its all in main().

#include <string.h>
#include <stdio.h>
#include "input.c"

FILE *playerdata;

int main(void)
{
	int buffer = 11;
	char name[buffer];
	printf("Please Enter Your Name\n");
	printf("Maximum Length 10 characters\n");
		
	fgets(name,buffer,stdin); 
	playerdata = fopen(name, "W+"); /*create the new file*/
	fgets(name,buffer,playerdata); /*write the players name to the file*/
	fclose(playerdata); /*close the file*/
}

thanks in advance for any help,

-Matt

Hi Again,

Well i'm bored and i've decided to do some coding just for practice and i've decided to work on a text based RPG, its only going to be basic due to my own knowledge but i think its doable. unfortunatly i've come accros a problem already and i've only writen a few lines of code.

The code deals with creating new character data, i plan to have all the character data stored in a text file, when the user starts a new game the program will ask for the character name then create a text file with this name write the players name and then close the text file.

heres the code that i have so far, this will eventualy be put into its own seperate function but for now its all in main().

#include <string.h>
#include <stdio.h>
#include "input.c"

FILE *playerdata;

int main(void)
{
	int buffer = 11;
	char name[buffer];
	printf("Please Enter Your Name\n");
	printf("Maximum Length 10 characters\n");
		
	fgets(name,buffer,stdin); 
	playerdata = fopen(name, "W+"); /*create the new file*/
	fgets(name,buffer,playerdata); /*write the players name to the file*/
	fclose(playerdata); /*close the file*/
}

thanks in advance for any help,

-Matt

should be fputs to write to a file

Thanks Gerard, but my program still crashes after the input of the data. so i assume there must still be problems in the code :(

You should check playerdata to be sure its valid

i.e is it NULL or valid

if (!playerdata)
{
        do something if not valid
}

plus you should return 0 in the main function

You should check playerdata to be sure its valid

i.e is it NULL or valid

if (!playerdata)
{
        do something if not valid
}

plus you should return 0 in the main function

surely playerdata shouldn't be null because i'm asking the C program to create the file.

the "W+" command opens the file for reading and writing, if it doesnt exist creates new if it does makes it blank.

and thanks for the return, totally forgot about that :)

playerdata = fopen(name, "W+");

should be

playerdata = fopen(name, "w+");

i swear i'm still half asleep lol, another error fixed but, still crashes soon as i press enter after typing in name.

Just a thought though, could it be that the string being recorded is "Name (return key)" and thats whats causing the error?

-Matt

#include <string.h>
#include <stdio.h>

FILE *playerdata;


int main(void)
{
	int buffer = 11;	
	char name[buffer];
	printf("Please Enter Your Name\n");
	printf("Maximum Length 10 characters\n");
	fgets(name,buffer,stdin);
	playerdata = fopen(name, "w+"); /*create the new file*/
	if (!playerdata)
	{
		printf("it failed!\n");
	}
	else
	{
		fputs(name,playerdata); /*write the players name to the file*/
		fclose(playerdata); /*close the file*/
	
	}

	
	
	return 0;
}

I copied your code and ran it like this...and it worked!

How very peculiar, thanks gerard. I'm still wondering what i'd done wrong but it compiles and doesnt crash now so i'm happy :)

Thank you very much

-Matt

EDIT: 1 last problem, i dont know where its saved the file, its not in the project folder any ideas?

EDIT2: Stupid me :( its not saved the file because its not creating the file.

It's because you can't create a buffer using a variable as the size:

int buffer = 11;
	char name[buffer];

You need something like:

#include <string.h>
#include <stdio.h>
#include "input.c"

#define BUFFERSIZE  11

FILE *playerdata;

int main(void)
{
	char name[BUFFERSIZE];
	printf("Please Enter Your Name\n");
	printf("Maximum Length 10 characters\n");

And what if her name is Elizabethany? :)

commented: Thanks for making me realise the problem :) +1

So Walt what you are saying...is that C doesn't support dynamic arrays????

>So Walt what you are saying...is that C doesn't support dynamic arrays????
Not in that way. If you want to dynamically allocate memory, you would have to use malloc or calloc to allocate a chunk of memory, and then once done using it, deallocate it with free().

Well John A what about

#include <stdio.h>

void myfunc(int x);

int main (int argc, char**argv)
{
	int x = 123;
	myfunc(x);
	return 0;
}

void myfunc(int x)
{
	int iarray[x];
}

And what if her name is Elizabethany? :)

it would work lol, if the name is 10 characters long or more it creates the file perfectly. So my guess is that when i press return to store the name it also stores the rtn character which is what is making the program crash


Thanks walt :)


EDIT

int newgame()
{
	char name[BUFFER];
	int length;
	
	printf("Please Enter Your Name\n");
	printf("Maximum Length 10 characters\n");
	fgets(name,BUFFER,stdin);
	length = strlen(name); /*find the length of the name*/
	if (name[length-1] == '\n') /*check if last character is new line char*/
	{
		name[length-1] = 0; /*remove the newline char*/
	}
	playerdata = fopen(name, "w+"); /*create the new file*/
	if (!playerdata)
	{
		printf("Unable to Create File\n");
		return 1;
	}
	else
	{
		fputs(name,playerdata); /*write the players name to the file*/
		fclose(playerdata); /*close the file*/

	}
}

thats solved it :)

So after you read a name, check the last character in the name and if it's a \n change it to \0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.