954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Where Is The New File I Created?

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);
}
ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
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.outis the executable file. Look in the same directory as a.out, or the current working directory when you run your program.

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

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?)

ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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);
	}
ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 

Who said //?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Ah, well this website did: http://www.mycplus.com/tutorials/c-programming-tutorials/file-handling/
But a single / did work. Thank you!

ohblahitsme
Newbie Poster
11 posts since Jan 2009
Reputation Points: 15
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You