Hello. I've been working on this program that reads input from the user and attempts to put it in a file called profile.txt

I've got it to work (I checked this by making it output the file to the terminal), and from what I can tell, it does create the file, but I have no idea where it is. I'm thinking that maybe it temporarily creates it and then the file is destroyed once the program is finished running.

Here's my code:

//
//
// This is a test to see if I know how to write a program in C
// It will gather information from a user
// It will calculate how many years left for the user to become of legal age
// Create a user profile and save it in a file. 

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

//declaring my functions
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()
{

	//run checkForAccounts() first to…check for accounts
	//this doesn't actually check for any accounts yet
	checkForAccounts();
	
	//return the muthafuckin profile, yo
	returnProfile();

	readFileProfile();
	return 0;
}

//these function names are pretty self-explanatory
void checkForAccounts()
{
	char response[2];
	//this part checks for an account
	//there is always an account right 
	//now because n is always = to 1
	int n = 1;

	if(n == 1)
	{
		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);
		}
	}

}

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

	printf("Enter your age: \n");
	scanf("%d", &age);
	fpurge( stdin);

	profile = fopen("PROFILE.TXT", "w+");
	if (!profile)
	{
		printf("Error creating file.");
	}
	else
	{
		fprintf (profile, "%s", name);
		fprintf (profile, "%d", age);
		fclose (profile);
	}

}

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

int readFileProfile()
{
	int c;
	profile = fopen("PROFILE.TXT","r");
	c = getc(profile);
	while (c!= EOF)
	{
		putchar(c);
		c = getc(profile);
	}
	fclose(profile);
}

It's in the same directory as the .EXE file.

Thanks for the reply WaltP, but I don't have an .EXE file. I'm running a.out from the terminal in order to execute my program.

Okay, then it should be in the same directory as a.out. It gets created on line 82. If it doesn't, line 85 should print. Have lines 89 and 90 print to standard out just to confirm they are executed. Add a cin.get() or two after line 90 to pause the program, then open another command prompt in the folder that a.out is in and execute an "ls -a" and see if that file is there.

Thanks for the reply WaltP, but I don't have an .EXE file. I'm running a.out from the terminal in order to execute my program.

a.out is the executable file. Look in the same directory as a.out, or the current working directory when you run your program.

Oh I see now. Thanks a lot! I found it in my user directory (where my terminal starts from. I'm not sure what it's called. Is that my user folder? Or is it called root?)

Thanks for the reply WaltP, but I don't have an .EXE file. I'm running a.out from the terminal in order to execute my program.

Since 99% of the posters here use Windows, forgive me for not realizing you are running on Linux.

No worries. I apologize if I sounded rude.

I do have another question though. The file that it creates doesn't appear in the same directory as my executable. I've tried specifying a path so that it looks like "\\Documents\\profile.txt" but all this does is make the file called\Documents\profile.txt in the same place that it normally does (my user folder). Is this an OS specific problem or something else? I'm using OS X at the moment, by the way.

For the record, my code looks like this:

profile = fopen("\\Documents\\profile.txt", "w+");
	if (!profile)
	{
		printf("Error creating file.");
	}
	else
	{
		fprintf (profile, "%s", name);
		fprintf (profile, "%d \n", age);
		fclose (profile);
	}

Yes. \ is for Windows. / is for Linux.

When I change it to a "//" it runs line 85 and doesn't create a file.

Who said //?

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.