1

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;
}
Zillua
  • 11
  • 1
  • 5
  • 1
    Unrelated to your question, but I suggest you do some research about *emulating passing arguments by reference in C*. – Some programmer dude May 19 '18 at 17:40
  • 1
    Oh, and never ***ever*** use `gets`. It's a dangerous function that have long been obsolete, and even has been removed from the current C standard. – Some programmer dude May 19 '18 at 17:41
  • I heard about fgets, but idk how to remove the \n at the end of the line – Zillua May 19 '18 at 17:45
  • Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane May 19 '18 at 18:01

1 Answers1

1

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 (i.e. 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 (e.g. 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.

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • Thank you so much for responding, I thought no one would respond, and I would be stuck trying to find out what to do for this by myself. Yea I was planning to do something like what you had written. If you don't mind, how would I read the username from a file back into the program. Like I only know the basics in files (reading, writing, appending). And is there a way I can find out more info on file login.txt. Also what is JSON, like is there a link because I have no prior knowledge about it. – Zillua May 20 '18 at 02:10
  • @KevinKannammalil Hey Kevin - to read the username back into the program, you just need to read the file contents into a string (which you indicate you know how to do). The string will then contain the username that you previously stored in the file. login.txt is just the file where you store and load this information... I just chose the name "login.txt" randomly as an example... you can save/load the username to whatever file name you want. Does this answer your question? – Jack Humphries May 21 '18 at 08:36
  • @KevinKannammalil As for JSON, I'm rethinking the advice I gave you since I imagine that you're not allowed to include any open-source code from the Internet in your program. I would just store the username and password in the file using the primitive method I mentioned in my answer (`username,password`). Just make sure that the user can't use commas in their username and password (you should ask them to enter in a new username/password if they try to include commas). – Jack Humphries May 21 '18 at 08:37
  • Hi, about reading in the files from a string, I only know about fseek () and SEEK_SET and SEEK_END. So I'm restricted to reading text based on the length from the beginning and the end. I dont know how long the username and password is, so how would I read them in. I could put those two in two diff text files one for username and one for password, but I don't know if that's good or not. Also when you said about storing in the variables into the file, "file login.txt: username,password" could you explain more about that, like do I need to add anything before or after it? – Zillua May 21 '18 at 17:49
  • @KevinKannammalil You can store them each in an individual file. So you could have username.txt that has the username and password.txt that has the password. You could store the length of each at the beginning of the file (e.g. `6,example`) if you have the restrictions you mentioned in your comment. So an example for username.txt would be `6,example`. – Jack Humphries May 22 '18 at 04:42
  • But I don't know how to save the variables into the file. Like I tried the format you said but it's not working, it's probably because I did it wrong. This is what I did: FILE * fPointer; fPointer = fopen("Username.txt", "w"); file username.txt: (6,username);; fclose(fPointer); – Zillua May 22 '18 at 11:51
  • @KevinKannammalil You can use the `fprintf` or `fputs` functions to write to a file. – Jack Humphries May 23 '18 at 01:26