This is a code i made, for checking if a nickname and password exists..
I have two question about this code...

1) In line 38 i close "fuser", but if i try to use this again there is an error. So, i used "fpass". Is there any way to use the same fot both files and not to create a new one?

2) When i check if the username exists, i get the line that the username is. Then i finde the password tha the user gives and i get the line if the pass exists. Finally i check if the number of these two lines are the same, so the user can loggin. But i thought that we can change it a little so, that it will have only one loop. What i thought is that we don't have the second while, but insteand an if, that checks if the password in the specific line that we got from the first while is the same with the one that the user gave at the beggining. Can we do this?
(I hope you understood what i said and what i ment :S )

#include <fstream>
#include <iostream>
#include <conio>
#include <string>

#pragma hdrstop
#pragma package(smart_init)

using namespace std;

void CheckLoginRequest(string username, string password);

main()
{
        string user,pass;
        cout<<"Give nickname";
        cin>>user;
        cout<<"Give password";
        cin>>pass;
        CheckLoginRequest(user,pass);
        getch();
}

void CheckLoginRequest(string username, string password)
{
        int linename=0,linepass=0;
        string str;
        int foundname=0,foundpass=0;
        ifstream fusers("usernames.txt",ifstream::in);
        while(getline(fusers,str) && foundname==0)
        {
                if (username==str)
                {
                        foundname=1;
                }
                linename++;
        }
        fusers.close();
        if(foundname==1)
        {
                ifstream fpass("passwords.txt",ifstream::in);
                while(getline(fpass,str) && foundpass==0)
                {
                        if (password==str)
                        {
                                foundpass=1;
                        }
                        linepass++;
                }
                fpass.close();
                if(foundpass==1)
                {
                        if(linepass==linename)
                        {
                                cout<<"you are now logged in"<<endl;
                        }
                }
                else
                {
                        cout<<"wrong password,try again"<<endl;
                }
        }
        else
        {
                cout<<"wrong username,try again"<<endl;
        }
}

1) You can close and re-open a file stream, but if you read the previously opened stream to end-of-file or there was an error, you need to clear the stream state:

#include <fstream>
#include <iostream>

void read_stream ( std::istream& in )
{
  char ch;

  while ( in.get ( ch ) )
    std::cout.put ( ch );
}

int main()
{
  std::ifstream in ( "test1" );

  if ( in ) {
    read_stream ( in );
    in.close();
    in.clear(); // Clear the stream state
  }

  in.open ( "test2" );

  if ( in )
    read_stream ( in );
}

The thing you need to remember is that the state of the stream buffer and the file the buffer is attached to are separate entities. Simply closing the file (disconnecting the stream buffer from the file handle) does not affect the buffer state. Any errors will still be active. If you compile and run the code as-is (changing the file names, of course), both files will be displayed. If you comment out the in.clear(); line and run again, only the first file will be displayed.

2) You can do that, but it sounds brittle to me if you continue using two files. I would prefer a single file where the user name and password (possibly encrypted) are on the same line. That way you can do something like this:

#include <fstream>
#include <sstream>
#include <string>

bool validate_user ( const std::string& user, const std::string& pass )
{
  std::ifstream in ( "users.txt" );
  bool is_valid = false;

  if ( in ) {
    std::string line;
    
    while ( std::getline ( in, line ) ) {
      std::stringstream split ( line );
      std::string legit_user;
      std::string legit_pass;

      if ( split>> legit_user >> legit_pass
        && ( user == legit_user && pass == legit_pass ) )
      {
        is_valid = true;
        break;
      }
    }
  }

  return is_valid;
}

are the user names and passwords in two different files ??? Normally they would be in the same file and on the same line. such as troula mypassword

Thank you both of you for your help..
I think that i am going to use the second way, were we have both usernames and passwords at the same file...

Can i ask you sth else... Can we convert the file, so that it won't be accesable?? If we can, what form would that be?

>Can we convert the file, so that it won't be accesable??
I suppose you mean not accessible by random users but fully accessible by your validation code. I'd suggest encryption of some sort.

Yes, that is exactly what i mean...
How can we do this? Do we have to use any specific programm?

Well, you have to be able to decrypt the data when you read the file, so most likely you'd want to have a function or two in your code to encrypt and decrypt. Search Google and you can find all kinds of information. I'd suggest starting with something simple, like Rot-13 to get a feel for things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.