简体   繁体   中英

How to store the value of a variable in a file in C

I need to make a journal (basically a program to read and write to files), with a login system, so I need to save the login information into a file. I dont know what the username and password would be since the user puts that, and I want to store that variable into a file. Also I was wondering how I could access that value after. Like say I store the variable username into a file, and I end the program, then I run it again, how would I be able to access the username so I can verify that the user that started the program is entering the right username. This is my final project for my Grade 12 course, and we don't have a proper comp sci teacher, so everything I know is self taught. So I'm not that comfortable with pointers, and pretty new to files. If you guys have any links which you recommend for me to learn these two topics from, it'll be much appreciated. Thanks for reading. The code that I got so far:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void signUp (char username [SIZE], char password [SIZE], char name [SIZE], int age)
{
char null;
printf("\nSign Up Procedure: \n");
scanf("%c", &null);
printf("Please enter your username: ");
gets(username);
printf("Please enter a password: ");
gets(password);
printf("Enter your full name: ");
gets(name);
printf("Enter your age: ");
scanf("%d", &age);

printf("\nUsername: %s, Password: %s, Full Name: %s, Age: %d", username, password, name, age);
}

void logIn(char username [SIZE], char password [SIZE])
{

}

int main()
{
printf("1. Log In\n");  // Enter 1 for logging in
printf("2. Sign Up\n"); // Enter 2 for signing up
int choice;
printf("Choice: ");
scanf("%d", &choice);

char username [SIZE];
char password [SIZE];
char name [SIZE];
int age;

int keepGoing = 0;
while (keepGoing == 0)
{
    switch (choice)
    {
        case (1):
            logIn (username, password);
            keepGoing = 1;
            break;
        case (2):
            signUp (username, password, name, age);
            keepGoing = 1;
            break;
        default:
            printf("Please enter a choice from the given options");
            keepGoing = 0;
            break;
    }
}


FILE * pfile;

pfile = fopen("Planner.txt", "w");

fputs("Hello World", pfile);
fputs("%s", username);

fclose(pfile);

return 0;
}

I'm sorry to hear that your teacher isn't much help. Here's a high level overview of what you should do for this project. Note that I am just recommending these steps given the instructions your teacher gave you for your project. If you were producing this program with the intent that it be used by others (ie not as a one-time school project), you would definitely want to use encryption to ensure that the journal contents and the user login information could not be read by others.

In order to store the username and password, you need to store them in a file after the user "registers" with your program, and then when you want to allow the user log in at a later time, you need to read the username and password back from the file and compare them to what the user typed in. Remember that the username and password are just strings, so you just need to call the correct functions to write them to a file on your hard drive... you don't need to do anything more difficult than that. The file name can just be something you hardcode into your program (eg login.txt).

When you store the username and password in a file, you need to store them in a specific format so that you're able to differentiate between the two. For example, one primitive way of doing so would be the following:

file login.txt: username,password

When you read the contents of login.txt in your program, you know that everything before the comma is the username and everything after the comma is the password. However, this format is not ideal because if the user includes a comma in his/her username or password, then your program will break. For a better way of storing this information in a file, I encourage you to read about JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM