Hello,

Now, this may sound stupid, as it concerns basic file I/O, but my code doesn't work correctly- at least it isn't doing what I want it to. I wanted it to write a file the first time it runs, which it does perfectly, and every time it is run after that, it should read the written file and store values off it(like username and password) and correlate it to validate user input. However, I am facing some problems, which is very likely to be an elementary one, about how I am not getting the required values after reading the file.
for example: the file is as following
1 admin adminpass
where the three columns are id, admin username and admin password. It writes it perfectly, reads it as well, as I get no compilation error and no runtime error as well.

Here is the code:

#include <iostream>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;

//////////////////////////Defenitions and Standards///////////////////////////
#define adminfile "admin_info.dat"
#define none 0

const int NotUsed = system("color 1B");

unsigned int id;
int q, first_run(), i=1;//Integer Functions and variables
double w=-65000;//Double Variables
void generate_choices(), admin_validator();//References Modules used
void long_slashes(), small_slashes(), smallest_slashes();//References Slashes
void check_admin(unsigned int, const char, char);
string userstring, passstring, fullstring;//Other Global variables
char usrnm, passwd;
char pwd, uname, password, username;
////////////////////////End Defenitions and Standards/////////////////////////

//////////////////////////////All fstream/////////////////////////////////////
ifstream fin(adminfile, ios::in | ios::out | ios::app);
ofstream fout;

/*fstream used in main()*/fstream file(adminfile, ios::in);

////////////////////////////End All fstream///////////////////////////////////

////////////////////////////////////Main/////////////////////////////////////
int main()
{
    clock();
    string choice;    
    
    if((file.fail())||(file.peek() <= none))
    {                   
    //////////////////////////////////////////////////////////////////////////
    
    /*   CONFIGURATION MANAGER
               This checks for the config ini file, described below in the 
               configuration module.
    */    
    small_slashes();
    smallest_slashes();    
    check_admin(id, username, password);
    cout << "\n\n";
    smallest_slashes();
    small_slashes();
    for(; w<=1000000; w++);
    system("cls");
    /*   END CONFIGURATION MANAGER
               This describes the end of Config manager, and is defined in
               the configuration module.
    */
    }
    
    
    //////////////////////////////////////////////////////////////////////////
    
    /*   ADMIN VALIDATOR
               This checks whether the password as defined in config matches
               with what the user entered.
    */
    /*cout << "\n\n\t\t";
    smallest_slashes();    
    admin_validator();
    cout << "\n\n\t\t";
    smallest_slashes();
    for(w=-65000; w<=1000000; w++);
    system("cls");
    /*   END ADMIN VALIDATOR
               This describes the end of the Admin Validator, whose function
               is described in the modules below.
    */
    
    ///////////////////////////////////////////////////////////////////////////
    
       
    long_slashes();
    cout << "\n\n\t\t\t\tThe AdminTool\n\n\t\t\t\t\t\tV1.0\n\n";
    long_slashes();
    
    cout << "\n\n\t";    
    
    cout << "\n\n\tWelcome to the AdminTool!\n\n\t";
    generate_choices();
    
    cout << "\n\n\tWhich of these would you like to do? (1- ): ";
    cin >> choice;    
    
    cout << pwd << "\t\t" << uname; 
    system("pause");
    return 0;
}
//////////////////////////////////End Main///////////////////////////////////


//////////////////////////////////Slashes///////////////////////////////////
void long_slashes()
{
     for(q=1; q<=70; q++)
     {
              cout << "/";
              }     
}

void small_slashes()
{
     for(q=1; q<=45; q++)
     {
              cout << "#";
              }
}

void smallest_slashes()
{
     for(q=1; q<=35; q++)
     {
              cout << ".";
              }
}

/////////////////////////////End Slashes//////////////////////////////////////

/////////////////////////////Configuration Checks/////////////////////////////
    
void assign(unsigned int id, const char username, char password)
{
     uname=username;
     pwd=password;
     cout << setiosflags( ios::left ) << setw( 10 ) << id 
        << setw( 13 ) << uname << setw( 7 ) << setw( 12 )
        << resetiosflags( ios::left )        
        << pwd << '\n';
}

void check_admin(unsigned int id, const char username, char password)
{
            
    if(fin.fail()) //File error
    {
        first_run();
        }
        else if(fin.peek() <= none)
        {
            first_run();
            }
            else
            {
                cout << setiosflags( ios::left ) << setw( 10 ) << "ID" 
                << setw( 13 ) << "Username" << "Password\n"
                << setiosflags( ios::fixed | ios::showpoint );
                
                while ( id >> username >> password )
                {
                      assign(id, username, password);
                      }                                               
                fin.close();
                fin.clear();
            }
            
}

int first_run()
{    
     int numch;
     
     
     cout << "\n\t";
     cout << "\n\n\tHow many accounts would you like to create? ";
     cin >> numch;
     cout << "\n\n\t";
     
     
     fout.open(adminfile , ios::out | ios::app );
     for(; i<=numch; i++)
     {
           cout << "\n\n\tPlease Enter a new username: ";
           cin >> usrnm;
           cout << "\n\n\tPlease Enter a new password: ";
           cin >> passwd;  
           userstring=usrnm+" ";
           passstring=passwd;           
           fullstring=" "+userstring+passstring+"\n";
                  
           fout << i << fullstring;
           }
     //Close and clear the file write buffer
     fout.close();
     fout.clear();
     cout << "\n\n\tSuccess!\n\n\t";
     //And reopen the file
     fin.open(adminfile , ios::in );
     
     
     return 0;
}
//////////////////////////End Configuration Checks////////////////////////////

///////////////////////////////Admin Validator////////////////////////////////
void admin_validator()
{
     char adminname[120], adminpass[100];
     
     reDo:
          
     cout << "\n\n\t\tPlease Sign in Below:\n\n\t\t";
     cout << "Username: ";
     cin >> adminname;
     
     cout << "\n\n\t\tPassword: ";
     cin >> adminpass;
     
     cout << "\n\n\t";
     
     /*if((adminname==uname))
     {
                              }
                                                else
                                                {
                                                    cout << "\n\t\tError!!\n\n";
                                                    goto reDo;
                                                    }*/
}
/////////////////////////////End Admin Validator//////////////////////////////

///////////////////////////////Modules////////////////////////////////////////     
void generate_choices()
{
     cout << "1)\tEdit your Info\n\n\t";
     cout << "2)\t.........\n\n\t";
}

Is the code wrong or have I done something illogical? Am I using the wrong method? Or is it the program? Any help would be well appreciated.

Thanks in Advance

PS: I know I am not the best in considering computer memory whilst programming.

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

A quick glance, looks like you need help with string comparison and in good need of a simple file i/o tutorial.

A quick glance, looks like you need help with string comparison and in good need of a simple file i/o tutorial.

Thanks for the reply, but where do I find a good tutorial on the above?

Member Avatar for iamthwee

Okay if you're using strings use them ALL the way through your program. That way your code will be consistent as well as less prone to bugs/ memory leaks.

Secondly std::strings use the header file #include<string> not #include <cstring>

File i/o would be:

Read

ifstream read("test.txt");

string line;

while(getline(read, line, '\n'))
{
  cout << line <<endl;
}
read.close();

write

ofstream write("test2.txt");

write << "Boo!"<<endl;

write.close();

Thanks again iamthwee. You mean using the strings instead of char arrays right?

Member Avatar for iamthwee

Thanks again iamthwee. You mean using the strings instead of char arrays right?

Yes that is correct.

commented: Thanks for the post. I will start correcting it right away. Again Thanks! +1

I made some changes to the code, but now the problem seems to be opening the file. Again, I am mystified, probably as its quite late, but still, could anyone point out where the Code fails? I have tried debugging it and it points to this line - "ifstream read(adminfile);". I can't make sense of this problem.

Here's the new, although not necessarily improved code:

#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;

//////////////////////////Defenitions and Standards///////////////////////////
#define adminfile "admin_info.dat"
#define none 0

const int NotUsed = system("color 1B");

unsigned int id;
int q, first_run(), i=1;//Integer Functions and variables
double w=-65000;//Double Variables
void generate_choices()/*, admin_validator()*/;//References Modules used
void long_slashes(), small_slashes(), smallest_slashes(), check_admin()/*, admin_validator()*/;//References Slashes
string userstring, passstring, idstring, password, username, fullstring;//Other Global variables
string temp1, temp2, temp3; 
////////////////////////End Defenitions and Standards/////////////////////////

//////////////////////////////All fstream/////////////////////////////////////
ifstream read(adminfile);//This is the problem code!
ofstream write;
////////////////////////////End All fstream///////////////////////////////////

////////////////////////////////////Main/////////////////////////////////////
int main()
{
    clock();
    string choice;    
    
                     
    //////////////////////////////////////////////////////////////////////////
    
    /*   CONFIGURATION MANAGER
               This checks for the config ini file, described below in the 
               configuration module.
    */    
    small_slashes();
    smallest_slashes();    
    check_admin();
    cout << "\n\n";
    smallest_slashes();
    small_slashes();
    for(; w<=1000000; w++);
    system("cls");
    /*   END CONFIGURATION MANAGER
               This describes the end of Config manager, and is defined in
               the configuration module.
    */
    
    
    
    //////////////////////////////////////////////////////////////////////////
    
    /*   ADMIN VALIDATOR
               This checks whether the password as defined in config matches
               with what the user entered.
    */
    /*cout << "\n\n\t\t";
    smallest_slashes();    
    admin_validator();
    cout << "\n\n\t\t";
    smallest_slashes();
    for(w=-65000; w<=1000000; w++);
    system("cls");
    /*   END ADMIN VALIDATOR
               This describes the end of the Admin Validator, whose function
               is described in the modules below.
    */
    
    ///////////////////////////////////////////////////////////////////////////
    
       
    long_slashes();
    cout << "\n\n\t\t\t\tThe AdminTool\n\n\t\t\t\t\t\tV1.0\n\n";
    long_slashes();
    
    cout << "\n\n\t";    
    
    cout << "\n\n\tWelcome to the AdminTool!\n\n\t";
    generate_choices();
    
    cout << "\n\n\tWhich of these would you like to do? (1- ): ";
    cin >> choice;    
    
    
    system("pause");
    return 0;
}
//////////////////////////////////End Main///////////////////////////////////


//////////////////////////////////Slashes///////////////////////////////////
void long_slashes()
{
     for(q=1; q<=70; q++)
     {
              cout << "/";
              }     
}

void small_slashes()
{
     for(q=1; q<=45; q++)
     {
              cout << "#";
              }
}

void smallest_slashes()
{
     for(q=1; q<=35; q++)
     {
              cout << ".";
              }
}

/////////////////////////////End Slashes//////////////////////////////////////

/////////////////////////////Configuration Checks/////////////////////////////
    
void check_admin()
{
     if(read.fail()) //File error
     {
        first_run();
        }
        else if(read.peek() <= none)
        {
            first_run();
            }
            else
            {
                                        
                       for(int i=1; i<=3; i++)//As there are only 3 columns in file
                {
                       cout << "\n";
                       for(int j=1; j<=i; j++)
                       {
                               while(getline(read, fullstring, ' '))
                               {
                                                   temp1=fullstring;
                                                   cout << temp1 << "\t";
                                                   break;
                                                     }
                                 }
                       for(int k=1; k<=i; k++)
                       {
                               while(getline(read, fullstring, ' '))
                               {
                                                   temp2=fullstring;
                                                   cout << temp2 << "\t";
                                                   break;
                                                     }
                                 }
                       for(int l=1; l<=i; l++)
                       {
                               while(getline(read, fullstring, ' '))
                               {
                                                   temp3=fullstring;
                                                   cout << temp3 << "\t";
                                                   break;
                                                     }                                                     
                                 }                
                         }
                read.close();
                read.clear();
            }
            
}

int first_run()
{    
     int numch;
     
     
     cout << "\n\t";
     cout << "\n\n\tHow many accounts would you like to create? ";
     cin >> numch;
     cout << "\n\n\t";
     
     
     write.open(adminfile , ios::out | ios::app );
     for(; i<=numch; i++)
     {
           cout << "\n\n\tPlease Enter a new username: ";
           cin >> username;
           cout << "\n\n\tPlease Enter a new password: ";
           cin >> password;
                  
           write << i << ' ' << username << ' ' << password;
           }
     //Close and clear the file write buffer
     write.close();
     write.clear();
     cout << "\n\n\tSuccess!\n\n\t";
     //And reopen the file
     read.open(adminfile , ios::in);
     
     
     return 0;
}
//////////////////////////End Configuration Checks////////////////////////////

///////////////////////////////Admin Validator////////////////////////////////
/*void admin_validator()
{
     string adminname, adminpass, adminid;
     
     reDo:
          
     cout << "\n\n\t\tPlease Sign in Below:\n\n\t\t";
     cout << "Username: ";
     cin >> adminname;
     
     cout << "\n\n\t\tPassword: ";
     cin >> adminpass;
     
     cout << "\n\n\t\tID: ";
     cin >> adminid;
     
     cout << "\n\n\t";
     
     if((adminname==temp2))
     {
                           if(adminpass==temp3)
                           {
                                               if(adminid==temp1)
                                               {
                                                                 cout << "Success!";
                                                                 }
                                               }
                           }
                                                else
                                                {
                                                    cout << "\n\t\tError!!\n\n";
                                                    goto reDo;
                                                    }
}*/
/////////////////////////////End Admin Validator//////////////////////////////

///////////////////////////////Modules////////////////////////////////////////     
void generate_choices()
{
     cout << "1)\tEdit your Info\n\n\t";
     cout << "2)\t.........\n\n\t";
}

PS: The old problem has been sorted out, I am merely posting this in the same thread.

I don't know if it solves your problem, because the problem isn't really clear.

When I worked with the ifstream I had problems with the file IO to.
The solution was to NOT declare the ifstream as a global varaible, but everytime the ifstream is used make a new ifstream.

I hope this will help.

I don't know if it solves your problem, because the problem isn't really clear.

When I worked with the ifstream I had problems with the file IO to.
The solution was to NOT declare the ifstream as a global varaible, but everytime the ifstream is used make a new ifstream.

I hope this will help.

Thanks for the answer! It was indeed the globally declared ifstream that was the problem. Among other things, this code seriously needs some work!! Thanks for the advice, it did help a lot.

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.