C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 15th, 2003, 12:34 PM
mikebaily mikebaily is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 mikebaily User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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:ut | 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:ut | 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:ut | 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:os_type mark = f1.tellg();
f1.open(FileName.c_str(),ios:ut|ios::app);
f1.write((char*)&x,sizeof(x));
f1.clear();
f1.seekg(mark);
f1.close();

}
}


}

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

return;
}

Reply With Quote
  #2  
Old May 16th, 2003, 12:20 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > My program doesn't write records to a file - but it should!!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap