Hi everyone,
I'm trying to figure out how I can write information stored in a variable to a file (right now I have a text file, but that's probably not the best way to do it) and then be able to recall that information back to the variable after I load the file.

More specifically: I have a variable called "name" and when you run my program, it asks for your name and then stores it in a text file called profile. When you run the program again, it looks for the profile and then loads the characters from it. I want it to load those characters back into the variable "name" so that the program recognizes it as a variable.

The program writes the variables to the function at line 98 in the function newAccount()

Here is my code:

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

//declaring my functions
//legalAge() accepts 1 integer (age)
int legalAge (int age);
int returnProfile();
int readFileProfile();

//newAccount and checkForAccounts return nothing
void newAccount ();
void checkForAccounts ();

//defining global variables
int age;
char name[50];

//creates file called profile
FILE *profile;

int main()
{
	printf("Welcome! We are going to check for a profile. \n");
	printf("…\n");

	//run checkForAccounts() first to…check for accounts
	checkForAccounts();
    
    legalAge(age);
	
	return 0;
}

//these function names are pretty self-explanatory
void checkForAccounts()
{
	char response[2];
	//this part checks for an account

	profile = fopen("./profile.txt","r");
	if(!profile)
	{
		printf("No accounts found. Would you like to create an account? (y/n) \n");
		fgets (response, 2, stdin);
		fpurge (stdin);
		
		//if the user replies "y", create a new account
		//otherwise GTFO
		if(strcasecmp(response,"y") == 0)
		{
			newAccount();
		}
		else
		{
			printf("Uhh…okay. \n");
			exit(EXIT_SUCCESS);
		}
	}
	else
	{
		readFileProfile();

		printf("Would you like to run legalAge()?\n");
		fgets (response, 2, stdin);

		if(strcasecmp(response,"y") == 0)
		{
			legalAge(age);
		}
		else
		{
			printf("What would you like to do? \n");
		}
	}

}

void newAccount()
{
	
	printf("Enter your name: \n");
	fgets (name, 50, stdin);
	fpurge (stdin);

	printf("Enter your age: \n");
	scanf("%d", &age);
	fpurge( stdin);
	
	//this creates the file or displays an error message if the file can't be created
	profile = fopen("./profile.txt", "w+");
	if (!profile)
	{
		printf("Error creating file.");
	}
	else
	{
		fprintf (profile, "%s", name);
		fprintf (profile, "%d \n", age);
		fclose (profile);
	}

}

int returnProfile()
{
	printf("Your profile says: \n");
	printf("Name: %s", name);
	printf("Age: %d \n", age);
}

int readFileProfile()
{
	printf("Profile found! \n");
	int c;
	profile = fopen("./profile.txt","r");
	c = getc(profile);
	while (c!= EOF)
	{
		putchar(c);
		c = getc(profile);
	}
	fclose(profile);

}

The idea of saving data to a file to later load into program-related data fields is known as marshalling

Just reverse the write. Do a read instead.

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.