-3

im trying to validate user name, mail and password from "users.txt" file, im trying to use fgets, but when I ran it, it wouldnt recognize the information and, when I tried printing what it was actually validating, turned out that the strings it got from the fgets function were totally unexpected.

Data from users.txt:

  • jean
  • jn@gmail.com
  • jj123
  • alfredo
  • alf@gmail.com
  • somepass
  • robt
  • rob@gmail.com
  • rob111

As far as I know, fgets returns the string until it finds a \n character or it reaches the end of the file. what I actually got from printf:

jj123 jj123 jj123 robt robt robt rob111 (null) (null)

Code: `

void login(bool is_logged_in)
{
    printf("enter your name\n");
    string name_session = GetString();

    printf("enter your mail:\n");
    string mail_session = GetString();

    printf("enter your password:\n");
    string password_session = GetString();

    FILE *users_file;
    char getstring[100];
    users_file = fopen("users.txt", "r");
    if (users_file != NULL)
    {
        while (feof(users_file) == 0)
        {
            //added this to check what I was actually getting from file
            string text1 = fgets(getstring,100,users_file);
            string text2 = fgets(getstring,100,users_file);
            string text3 = fgets(getstring,100,users_file);
            printf("%s",text1);
            printf("%s",text2);
            printf("%s",text3);
            //3 times because the if statement is checking 3 strings

            if ((fgets(getstring,100,users_file) == name_session ) &&
                (fgets(getstring,100,users_file) == mail_session) &&
                (fgets(getstring,100,users_file) == password_session))
            {
                is_logged_in = true;
                fclose(users_file);
                menu();
            }
        }
        printf("You've failed your log in.\n");
        fclose(users_file);
        menu();
    }
    else
    {
        printf("¡¡ERROR!!: there's been a problem and the program will now exit.\n");
    }
}

`

IZenteno09
  • 13
  • 4
  • 1
    Your code is more C than C++. What is `string`? Are you `using std;`? – Basile Starynkevitch Jun 18 '17 at 05:12
  • Use C++ file streams ([`std::ifstream`](http://en.cppreference.com/w/cpp/io/basic_ifstream)-s) and [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) and [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) – Basile Starynkevitch Jun 18 '17 at 05:14
  • these are my libraries: #include #include #include – IZenteno09 Jun 18 '17 at 05:16
  • Also, compile with all warnings and debug info (e.g. `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) and **use the debugger `gdb`** – Basile Starynkevitch Jun 18 '17 at 05:17
  • 2
    If you're going to use `fgets` you should use it correctly. If you pass the same first parameter 3 times you're overwriting the first 2 strings. If `string` is a typedef for `char*` then your comparisons aren't going to work either. – Retired Ninja Jun 18 '17 at 05:17
  • So you are following a course on C programming, and you code in C++. I recommend using the right language and the right compiler (and debugger). C and C++ are different languages, choose one! If coding in C, compile with a C (not C++) compiler, e.g. `gcc -Wall -Wextra -g` and **use the debugger** – Basile Starynkevitch Jun 18 '17 at 05:18
  • 1
    You may have more success here: https://cs50.stackexchange.com/ I'd guess they're more familiar with the provided headers and such. – Retired Ninja Jun 18 '17 at 05:19
  • To compare C strings use [strcmp](http://en.cppreference.com/w/c/string/byte/strcmp), not `==` (which compare *addresses*) – Basile Starynkevitch Jun 18 '17 at 05:21
  • `while (feof(users_file) == 0)` checks for end of file before reading from the file. More on why that's bad here: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong . None of the file accesses test for success, yet the values are used anyway. It's easier to find errors when you have the code actively looking for them. – user4581301 Jun 18 '17 at 05:47

1 Answers1

0

Use fstream instead

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
   string line;
   ifstream file("users.txt");
   if(!file){
     cout<<"file do not exist!\n";
     return 0;
   }
   while(getline(file,line)){
     cout<<line<<endl;
   }
}
shahryar
  • 83
  • 5