0

Hello everyone i have to do simple login program in C, i have a problem to save username, full name and password because i need to choose '1' for sign up and '2' for sign in but always when i exit the console and try to read my file everything disappears.

#include <stdio.h>

int main() {

FILE *f = fopen("users.txt", "w+");

if (f == NULL) {
printf("N/A");
exit(1);
}

int choose,
username[15],
fullName[20],
password[15],
// confirmPassword[15];

printf("Welcome!\n");
printf(" 1: Sign up\n 2: Sign in\n");
printf("--------------------------------\n");
scanf("%d", &choose);

if(choose==1) {

    printf("Username: ");
    scanf("%s", &username);

    printf("Full name: ");
    scanf("%s", &fullName);  /// BECAUSE OF SPACE IT COUNTS LIKE A PASSWORD

    printf("Password: ");
    scanf("%s", &password);

    fprintf(f, "%s\n%s\n%s", username, fullName, password);

}

if(choose==2) {

    char c;

    printf("Username: ");
    while( c != EOF) {

        c = fgetc(f);
        printf("%c",c);

    }
}

fclose(f);

return 0;
}

I have to deal with HASH too but i will try that on my own. And help about /// comment !

jovkm
  • 27
  • 6
  • whats the problem with the comment? – Gerardo BLANCO Dec 14 '17 at 21:38
  • @GerardoBLANCO problem is when i type full name i have to use space then program save second word in &password – jovkm Dec 14 '17 at 21:41
  • looks a lot like https://stackoverflow.com/q/47816312/2410359 – chux - Reinstate Monica Dec 14 '17 at 21:48
  • 1
    What is the value of `c` when code executes `char c; ... while( c != EOF) {`? – chux - Reinstate Monica Dec 14 '17 at 21:49
  • Oooh, I know this one... Umm,.. wait... no... `:(` – David C. Rankin Dec 14 '17 at 21:51
  • that is deleted right now everything in (choose==2) but it actually shows everything in file – jovkm Dec 14 '17 at 21:52
  • 1
    The point being is you invoke *Undefined Behavior* attempting to access an uninitialized value. You would need `int c = fgetc (f);` to initialize it properly. As written, when you declare `char c;` there is know way of knowing what stray garbage value is as the memory location for variable `c` until it is assigned a value. Also, the type must be `int` as `char` will not match `EOF` on little-endian machines. – David C. Rankin Dec 14 '17 at 21:54
  • @DavidC.Rankin i got it, but how should i read an only first row in a file? – jovkm Dec 14 '17 at 22:01
  • Similarly with `while( c != EOF)` is undefined behaviour because the first time the loop is run, `c` is uninitialised. Ditto: should be `int c`. – Weather Vane Dec 14 '17 at 22:02
  • I suggest using `fgets` if any field includes a space. Remember that `fgets` stores the final newline in the string. – Weather Vane Dec 14 '17 at 22:06
  • @jovkm - Since you already have `Username`, `Full Name` and `Password` in memory, if `choose==2`, just `printf` the value to `stdout` instead of worrying about reading the file again. (agreed, you should use `fgets` instead of `scanf`), but if using `scanf` for `Full name`, then use `"%19[^\n]%*c"` instead of `"%s"` to handle spaces in the name while limiting the characters that can be read to `20-1 = 19` characters to save room for the *nul-terminating* character at the end.. – David C. Rankin Dec 14 '17 at 22:07
  • so i can make variable where i can save username length then put it in fgets? fgets(str, variable with length, FILE *f) ? – jovkm Dec 14 '17 at 22:10
  • Simply `fgets (fullname, sizeof fullname, stdin)` (there is no need to subtract `1` from the number when using `fgets` it always includes the *nul-terminating* character within the number you give it.) Also note `fullname` must be a character array declared in scope to use `sizeof array` for its size, if you pass `array` to a function it is converted to a pointer, so if you do `sizeof arr_in_func` (it is just `sizeof (a pointer)` -- usually 4-bytes on i686 or 8-bytes on x86_64) – David C. Rankin Dec 14 '17 at 22:11
  • @jovkm Me thinks you may have lost it again. Perhaps an [Example with Ideas](https://pastebin.com/Mj7WRrAu) may help. Note the username, fullname and password are saved in a `struct` in the example rather than 3 separate character arrays. – David C. Rankin Dec 15 '17 at 01:49

1 Answers1

0

As per previous comments, and with my idea, here is how you may fix the code: 1. use int c or unsigned int c to declare c variable. 2. set c tp be (whatever as long it is not EOF); 3. make do while loop that at the end checks for EOF.

VladP
  • 529
  • 3
  • 15