#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    1
    Rep Power
    0

    Exclamation My program doesn't write records to a file - but it should!!!


    Hi

    My program doesn't produce records on a file.

    I have listed the code does anyone know why it isn't?

    Thanks in advance for your help.

    The code:


    #include <windows.h>
    #include <conio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <fstream>

    using namespace std;

    struct PAYROLL {
    char fname[20];
    char lname[20];
    float pay;
    char password[20];
    }x;

    void CheckNameOnFile();
    void enter_password();
    void clear_screen();
    void MakeArrayBlank();
    char Name[20];
    char LName[20];

    string FileName = "c:/hello.dat";

    float payment;
    bool RunMore;
    HKEY hKey;
    char szBuf[10];
    bool RunOnce;
    char val[10];
    char c;
    char pass[10];
    int counter;

    fstream f1;


    bool Isthere =false;

    void main ()
    {


    DWORD dwSize=0;
    DWORD dwValue;
    DWORD valType =0;
    DWORD dwLen = sizeof(val);

    f1.clear();
    strcpy(Name,"");
    strcpy(LName,"");
    strcpy(x.fname,"");
    strcpy(x.lname,"");
    strcpy(x.password,"");

    x.pay=0;



    int choice;
    int counter=0;

    char password[20];

    //open registry to look for a key

    strcpy(szBuf,"true");

    dwSize = sizeof(dwValue);


    if(:: RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    "system",
    0,
    KEY_SET_VALUE | KEY_QUERY_VALUE,
    &hKey)==ERROR_SUCCESS)

    long rc = RegQueryValueEx(hKey,
    "PayRoll Flag",
    NULL,
    &valType,
    reinterpret_cast<unsigned char *>(val),
    &dwLen);


    if (strcmp (val,"true") ==0)
    RunOnce = false;
    else
    RunOnce = true;

    ::RegCloseKey(hKey);

    cout << RunOnce;

    if (RunOnce == true) //write the key if first time it is run
    {

    if(:: RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    "system",
    0,
    KEY_SET_VALUE | KEY_QUERY_VALUE,
    &hKey)==ERROR_SUCCESS)
    {
    if (::RegSetValueEx(hKey,
    "PayRoll Flag",
    0,
    REG_SZ,
    (LPBYTE) szBuf,
    strlen(szBuf) + 1))
    {
    ::RegCloseKey(hKey);
    return;
    }
    }

    }





    f1.open(FileName.c_str(),ios::out | ios::app);
    f1.write((char*)&x,sizeof(x));
    //writes to disk
    //f1.clear();
    //f1.close();


    cout << "**************************************************";
    cout << "\n \n \n \n";
    cout << "Enter your first name : ";
    fflush(stdin);
    gets (x.fname);
    cout << "Enter your last name : ";
    fflush(stdin);
    gets (x.lname);

    strcpy(Name,x.fname);
    strcpy(LName,x.lname);


    if (RunOnce == false)
    {
    Isthere=false;
    f1.clear();
    f1.clear();
    f1.open(FileName.c_str(),ios::in|ios::app); //opens file for reading
    f1.read((char*)&x,sizeof(x)); //reads file


    while (!f1.eof() && !Isthere==true)
    {
    if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    {
    Isthere=true;

    }
    else
    f1.read((char*)&x,sizeof(x));
    }

    f1.close();

    if ((Isthere == true) && (strcmp(x.password,"") !=0))
    {

    // f1.clear();
    //f1.open(FileName.c_str(),ios::in); //opens file for reading
    f1.read((char*)&x,sizeof(x)); //reads file

    cout <<"\n" "\n" "\n" <<"Enter your password : ";
    cin >> password;

    if ((strcmp(x.password,password) ==0))
    {
    clear_screen();
    cout <<"x.password is : " <<x.password <<endl;
    cout <<"password is : " << password << endl;
    cout <<"x.fname is : " << x.fname << endl;
    cout <<"Name is : " << Name << endl;
    cout <<"x.lname is : " << x.lname << endl;
    cout <<"LName is : " << LName << endl;
    _getch();
    }


    else
    {



    if (Isthere==false)
    {
    cout << "\n" <<"You must enter the correct password ";
    cout << "\n" <<"You have 3 attempts";
    cin >> password;

    counter = counter+1;
    if (counter==3)
    {
    exit(1);
    }
    }
    }
    }
    else
    {
    cout <<"x.password is : "<< x.password;
    cout <<"\n" "\n" "\n" "You must enter a password now : ";
    cin >> x.password;
    // f1.clear();
    f1.open(FileName.c_str(),ios::out | ios::app);
    CheckNameOnFile();
    cout <<"x.password is : " << x.password;
    }



    }

    if (RunOnce == true)
    {
    enter_password();

    cout << x.fname;
    }

    Isthere=false;

    //f1.open(FileName.c_str(),ios::in); //opens file for reading
    f1.read((char*)&x,sizeof(x)); //reads file


    clear_screen();

    do {

    //ERROR NEED WHILE LOOP TO FIND END OF FILE

    while (!f1.eof() && !Isthere==true)
    {
    cout <<x.fname;
    if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    {
    Isthere=true;
    }
    else
    f1.read((char*)&x,sizeof(x));
    }

    f1.close();

    cout <<"******************************";
    cout <<"\n" <<"This session is for";
    cout <<"\n" <<x.fname <<" " << x.lname;
    cout <<"\n" "\n";

    cout <<" \n "<< "1 : Enter Pay";
    cout << "\n "<< "2 : Display your details";
    cout << "\n " << "3 : Change the path to install information";
    cout <<" \n " << "9 : Quits the program";
    cout <<"\n" "\n" "\n";

    cin >> choice;

    if (choice == 1)
    {

    f1.open(FileName.c_str(),ios::out | ios::app);

    cout <<"\n" << "Enter pay : " ;
    cin >> x.pay;

    CheckNameOnFile();
    exit(1);//exit program - another fix would be to set choice to 9
    }

    else if (choice == 2)
    {
    Isthere = false;
    clear_screen();


    if ((strcmp(x.fname,Name)==0) && (strcmp(x.lname,LName)==0))
    {
    Isthere=true;
    payment=x.pay;
    strcpy(Name,x.fname);
    strcpy(LName,x.lname);
    fflush(stdin);
    cout <<"\n" <<"The following information is from disk";
    cout <<"\n" <<"**************************************************";
    cout <<"\n \n" <<"Your name : " << Name<< " " <<LName;
    cout <<"\n";
    cout <<"Your payment is : ";
    cout <<payment;
    cout <<"\n""\n""\n""\n";
    while (choice !=1)
    {
    cout<<"Enter 1 to return to the menu : ";
    cin >> choice;
    if (choice == 1)
    {
    clear_screen();
    }
    }


    }


    }

    else if (choice ==3)
    {
    cout <<"The installation path at the moment is : ";
    cout <<FileName;
    cout <<"Enter new installation path please :";
    cin >> FileName;
    cout <<"\n" "\n" "\n";
    cout <<"The path has been changed to : ";
    cout << FileName;
    }


    }
    while (choice !=9);

    return;
    }


    void enter_password()
    {
    clear_screen();

    cout << "Enter a password - max 10 characters";

    c=getch();

    while (c!='\r' && c!='\n' && counter!=10)
    {

    cout <<"*";
    c=getch();
    pass[counter] = c;
    counter++;
    }
    strcpy(x.password,pass);


    CheckNameOnFile();


    }

    void CheckNameOnFile()
    {

    Isthere=false;

    while(!f1.eof() && !Isthere==true)
    {
    if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    {
    Isthere=true;
    ios::pos_type mark = f1.tellg();
    f1.open(FileName.c_str(),ios::out|ios::app);
    f1.write((char*)&x,sizeof(x));
    f1.clear();
    f1.seekg(mark);
    f1.close();

    }
    }


    }

    void clear_screen()
    {
    system("CLS");

    return;
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    It's very unlikey someone will take the time to go through all your code to pick out the problem. They will however, help you if you break down the code and locate a few lines that should be working that arent.

    -andy
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo