Hello, am working on a program like a database

Here's the code:

#include <cstdlib>
#include <iostream>

using namespace std;

void addclient(void);
void choice (void);

      typedef struct client{
      int number;
      char name[35];
      char gender[8];  
      char address[50];
      char phoneno[15];
      char mobileno[10];
      struct client *next; 
      } client;
     client *firstnode,*currentnode,*newnode; 
      int clientid = 0;
      
int main(int argc, char *argv[])
{
    int count;
    int x;
    FILE *datafile;
      char *filename = "client.dat";
      char ch;
      firstnode = NULL;

      datafile = fopen(filename,"r");

      if(datafile)
      {
      
      
      currentnode = firstnode; 
      while(1) 
      {
     
      fread(currentnode,sizeof(struct client),1,datafile);
      if(currentnode->next == NULL) 
      break;
      currentnode->next = newnode; 
      
      currentnode = newnode; 
      }
      fclose(datafile); 
      clientid = currentnode->number;

      }

      do
      {
      fflush(stdin);
      cout<<"\nWelcome To Mobile service Database"<<endl;
      cout<<"-- -----------------------------"<<endl;
      cout<<"1 - Add a new Client"<<endl;
     
      cout<<"-- -----------------------------"<<endl;
      cout<<"Q - Save and quit\n";
    cout<<"\tYour choice:";
      ch = getchar();
      ch = toupper(ch);
      switch(ch) 
      {
      case '1':
      cout<<"Add a new Client\n";
      fflush(stdin);
      addclient();//call addNewcontact function
      break;
     

      case 'Q':
      cout<<"Save and quit\n";
      default:
      break;
      }
      }
      while(ch != 'Q');

      /*
       * Save the records to disk
       */
      currentnode = firstnode;

      if(currentnode == NULL)
      return(0); /*no data to write*/

      datafile = fopen(filename,"w"); /*open file to write*/

      if(datafile == NULL)
      {
      cout<<"Error writing to " <<filename;
      return(1);
      }
      /* Write each record to disk*/
      while(currentnode != NULL)
      {
      fwrite(currentnode,sizeof(struct client),1,datafile);
      currentnode = currentnode->next;
      }
      fclose(datafile); /*closes data file*/
      return(0);
      }
      
      



 void addclient(void) 
 
      {
      newnode = (struct client *)malloc(sizeof(struct client));
      if(firstnode==NULL)
      firstnode = currentnode = newnode;

      else
      {
      currentnode = firstnode; 

      while(currentnode->next != NULL)currentnode = currentnode->next;
    
      currentnode->next = newnode; 
      currentnode = newnode; 
      }
      clientid++;
      cout<<"Client ID " <<clientid<<"  ";
      currentnode->number = clientid;
      cout<<"\n";
      cout<<"Enter Client name: ";
      cin.getline(currentnode->name,35);
      
      cout<<"Enter Client gender: ";
      cin>>currentnode->gender;

      cout<<"Enter Client address: ";
      cin>>currentnode->address;

      cout<<"Enter Phone Number: ";
      cin>>currentnode->phoneno;

      cout<<"Enter Mobile Number: ";
      cin>>currentnode->mobileno;

      cout<<"staff added!\n";

      currentnode->next = NULL;
      }

when i run it, it works well, but when i close the program and open the text file
and then rerun the program, the program crash
any help PLEASE

thanks

Does the problem only occur when you are running the program with the .txt file open? There might be write conflict, it's like the warning you get while trying to remove a file while it's open in a program.

.

just notice that, when i run the program for the first time, i works well. but when i close it and re run it it crashe even i f i dont open the text file.

I think there it a program in the reading part

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.