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);
}