1

I have a function which checks for username and password. I have two text files, one for a single line username and the other for a single line password. I then read this data from txtfile and store them as 2 strings. Then I compare these with the username and password input by the user using the function which returns 1 for success. But my code gives the wrong output.

int compare(char C[], char E[])
{
    ifstream fil1;
    ifstream fil2;
    fil1.open("user.txt", ios::in);
    fil2.open("pass.txt", ios::in);
    char ch1, ch2;
    char B[50], F[50];
    int i1 = 0, i2 = 0;

    while (!fil1.eof())
    {
        fil1.get(ch1);
        F[i1] = ch1;
        i1++;
    }
    F[i1] = '\0';
    fil1.close();

    while (!fil2.eof())
    {
        fil2.get(ch2);
        B[i2] = ch2;
        i2++;
    }
    B[i2] = '\0';
    fil2.close();

    int flag;
    int X = strlen(E);
    int Y = strlen(B);
    if (X == Y)
    {
        if ((strcmp(C, F) == 0) && (strcmp(E, B) == 0))
            flag = 1;
        else
            flag = 0;
    }
    else
        flag = 0;
    return flag;
}

It gives the wrong output even when username and password are right. Where is the error??

David G
  • 94,763
  • 41
  • 167
  • 253
  • 3
    [Why `while(!feof(file))` is always wrong?](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) –  Oct 31 '13 at 17:39
  • Since you are using `ifstream` - have you tried to use `std::string`? It can save you from some C-string using errors. – Sergey Oct 31 '13 at 17:40
  • i use borland C++ . std::string does not work – user2942416 Oct 31 '13 at 17:41
  • 2
    @user2942416 Stop using Borland C++. Get a decent compiler/stdlib implementation, such as GCC/MinGW. Even MSVC does the job. –  Oct 31 '13 at 17:42
  • i dont hav a choice. i know its ancient but im supposed to my school work in borland/turbo – user2942416 Oct 31 '13 at 17:44
  • and with `std::string` it's safe to `string s; fil1 >> s;` – Sergey Oct 31 '13 at 17:44
  • @user2942416 I'm guessing it is not an option, but a school teaching *now* using Borland C++ or Turbo C/C++ is horrible. The language has changed a *huge* amount since then. I've seen *several* users reporting that they "had" to use it, so is it really that prevalent in schools? – crashmstr Oct 31 '13 at 17:58
  • It has been close to 20 years since I last used them, but doesn't Borland/Turbo C++ have a `getline`? It should be easier to use that instead of char by char. – crashmstr Oct 31 '13 at 18:22

3 Answers3

0

In simple words, eof() returns true only when we try to read past the end of file (more than size of file). So you should change the code as follows:

while(true)
{
  fil1.get(ch1);
  if(fil1.eof())
    break;
  F[i1]=ch1;
  i1++;
}
Deepak
  • 470
  • 1
  • 5
  • 15
  • try printing the arrays B and F, check whether opening of files succeeded or not. – Deepak Oct 31 '13 at 18:06
  • How do you take input from user, does username or password contain any space? Also there is no need of first checking the length of the strings, strcmp does the same job – Deepak Oct 31 '13 at 18:18
0

Consider you have "user.txt" with "user" text inside. You will have F = "userr" with double last symbol 'r' because fil.eof() returns true only when an attempt to read past the end of file was made. Correct loop is:

while(fil1.get(ch1)) {
  F[i1] = ch1;
  i1++;
}

Same for second loop.

Sergey
  • 406
  • 2
  • 6
0

If you use std::string, this:

 while (!fil1.eof())
 {
     fil1.get(ch1);
     F[i1] = ch1;
     i1++;
 }

Is turned into:

std::string F;
file1 >> F;

So replacing your program with the relevant code will result into:

#include <fstream>
#include <string>

bool compare(std::string C, std::string E)
{
    std::ifstream file1("user.txt");
    std::ifstream fil2("pass.txt");

    std::string B, F;

    file1 >> F;
    file2 >> B;

    int size1 = F.size(), size2 = B.size();
    bool flag = (C == F) &&  (E == B);

    return flag;
}

You also seem to be using int instead of bool. I don't know if this is because of the compiler you are using, but you should seriously think about upgrading if so.

David G
  • 94,763
  • 41
  • 167
  • 253