You can only do two things:
1) Save the input onto the disk.(Requires that you create a text-file.)
2) Save the program to RAM.(Data will disappear once the program has terminated.)
To save data to a file first you need to create a file pointer to point to a file we will use to save user input.
Also, we will use fgets() function instead of the scanf() function.
Full program to save user data:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMECAP 15
//Holds user data.
struct data {
char *password;
char *username;
}info;
/*
***********************
*****Program Start*****
***********************
*/
int main(void)
{
//File pointer.
FILE *fp;
/*
**********************
********MENU**********
**********************
*/
//Get username.
printf("\nWhat is your username?: ");
info.username = (char*)malloc(NAMECAP * sizeof(char)); //Dynamically allocate memory.
if(info.username == NULL)
{
printf("\nError: Out of memory.\n");
exit(0);
}
fgets(info.username, NAMECAP, stdin);
info.username[strlen(info.username)-1] = '\0'; //Remove '\n' from input.
//Get password.
printf("What is your password?: ");
info.password = (char*)malloc(NAMECAP * sizeof(char)); //Dynamically allocate memory.
if(info.password == NULL)
{
printf("\nError: Out of memory.\n"); //If failed.
exit(0);
}
fgets(info.password, NAMECAP, stdin);
info.password[strlen(info.password)-1] = '\0'; //Remove '\n' from input.
//Open file for appending, create a new one if it doesn't already exist.
if((fp = fopen("data.txt", "a+")) == NULL)
{
printf("\nCould not open file.\n\n"); //If failed.
exit (0);
}
fprintf(fp,"%s\n%s",info.username,info.password); //Write to file with fprintf().
printf("%s\n%s\n",info.username,info.password); //Output to screen.
//Close file.
fclose(fp);
//Free up memory.
free(info.password);
free(info.username);
//Exit program.
exit(0);
}