void Addcostumer(){
	struct CustomerData *p;
	char another ='y';
	char anothers ='n';
	CustomerData *newcous;
	char str1[80];
	char str2[100];
	char str3[20];
	//fseek(fc,0,SEEK_END);
	
	
	p=(struct CustomerData *)malloc(sizeof(struct CustomerData));
   
	newcous= new CustomerData;

	printf("Costumer Name:");
	fflush(stdin);
	gets_s(str1);
	strcpy_s(newcous->CName,str1);

	printf("Costumer Address:");
	fflush(stdin);
	gets_s(str2);
	strcpy_s(newcous->Address,str2);

	

	printf("Costumer Puhelin:");
	fflush(stdin);
	gets_s(str3);
	strcpy_s(newcous->Puhelin,str3);
	if ((fc = fopen("c:\\users\\Emmas4impact\\Documents\\customer.bin", "wb"))==NULL)
   {
      printf("Cannot open file for writing \n");
      exit(1);
   }
	fwrite(newcous,sizeof(struct CustomerData),1,fc);
  
   fclose(fc); 
   //Binary file reading
   if ((fc = fopen("c:\\users\\Emmas4impact\\Documents\\customer.bin", "rb"))==NULL)
   {
      printf("Cannot open file for reading \n");
      exit(1);
   }  
   fread(newcous, sizeof(struct CustomerData),1, fc);
   fclose(fc);   
	
	//fwrite(newcous,size,1,fc);
	
	printf("DO YOU WANT TO SUBMIT THIS FORM (Y/N)");
	fflush(stdin);
	
	if(another=='Y'|| another=='y'){
		//fwrite(newcous,sizeof(struct CustomerData),1,fc);
		another=getchar();
		exit(0);
		
	}else{
			
		//if(another=='y'||another=='Y')
		   another=getchar();
		   printf("DO YOU WANT TO ADD ANOTHER CUTOMER(Y/N)\n");
		   fflush(stdin);
		   if(anothers=='n'){
			   another=getchar();
			   newcous->Next=new CustomerData;
			   fwrite(newcous,sizeof(struct CustomerData),1,fc);
			   //break;
			  
		   }else
			    exit(1);
		    
		   
	}
	 
	
	//newcous->Next=NULL;
	customer();
	fclose(fc);
	
  
   
   
   
}

If it asked that do you want to submit this form and i type N for no it will exit so it doesn't allow me to add multiple record i don't know what's wrong with my function. Anyone with any hints?

First, don't use exit().

You need to add a loop to your program so that if you answer 'Y' then the program will just loop back to the beginning and start all over again

void addCustomer()
{
   char answer;
   do // beginning of loop
   {
      // put your code here

      printf("Add another record (Y/N)?\n");
      answer = getchar();

   } while( answer == 'Y'); // end of loop
   return 0; // exit program
}

You could also do it from main() and change addCustomer() to return the value of the answer from addCustomer().

char addCustomer()
{
   printf("Add another record (Y/N)?");
   return getchar();
}

int main()
{
  while( addCustomer() == 'Y')
       ; // just stay in this loop
}

Edited 5 Years Ago by Ancient Dragon: n/a

Now its add more than one record but it will erase the previous one added that way i only have one record in the file instead of multiple here is the new development code:

void Addcostumer(){
	struct CustomerData *p;
	char another;
	
	CustomerData *newcous;
	char str1[80];
	char str2[100];
	char str3[20];
	//fseek(fc,0,SEEK_END);
	
	
	p=(struct CustomerData *)malloc(sizeof(struct CustomerData));
   
	newcous= new CustomerData;
	do{
	printf("Costumer Name:");
	fflush(stdin);
	gets_s(str1);
	strcpy_s(newcous->CName,str1);

	printf("Costumer Address:");
	fflush(stdin);
	gets_s(str2);
	strcpy_s(newcous->Address,str2);

	

	printf("Costumer Puhelin:");
	fflush(stdin);
	gets_s(str3);
	strcpy_s(newcous->Puhelin,str3);
	if ((fc = fopen("c:\\users\\Emmas4impact\\Documents\\customer.bin", "wb"))==NULL)
   {
      printf("Cannot open file for writing \n");
      exit(1);
   }
	fwrite(newcous,sizeof(struct CustomerData),1,fc);
  
   fclose(fc); 
   //Binary file reading
   if ((fc = fopen("c:\\users\\Emmas4impact\\Documents\\customer.bin", "rb"))==NULL)
   {
      printf("Cannot open file for reading \n");
      exit(1);
   }  
   fread(newcous, sizeof(struct CustomerData),1, fc);
   fclose(fc);   
	
	//fwrite(newcous,size,1,fc);
	
	printf("Add another record (Y/N)?\n");
    another = getchar();
	newcous->Next=new CustomerData;
}while(another=='Y'); 
	
	//newcous->Next=NULL;
	customer();
	fclose(fc);
	
  
   
   
   
}

What you said is right but am having problem with the output its prints something like this:

C:\Users\Emmas4impact\Documents>type customer.bin
emma ■■■■■■■■■■■■■■■■■■■■the ■■■■■■■■■■■■■■■■■■■■■4567 ■■■■■■■══════Emman
uuuuu ■■■■■■■■■■■■■■Kuopio ■■■■■■■■■■■■■■■■■■09978234443 ══════Thejazjlf
■■■■■■■■■■■■■■■Finland ■■■■■■■■■■■■■■■■889120393 ■■══════
C:\Users\Emmas4impact\Documents>

i dont know while the daek space there.

And more also, The next name is not going to new line its continue from where it stop so i want it to always insert to a new line and the dark space should be leave blank.
Thanks.

Well, that's the way binary files work. The files contain non-printable characters so you can not treat them as if they are plain text files. You can not view binary files with a text editor such as Notepad. If you want to do that then you will have to change the way the file is written by calling fprintf() to write out each of the fields in the structure individually, such as fprintf(fp,"%s\n", newcous->str1);

Edited 5 Years Ago by Ancient Dragon: n/a

Thanks it works fine. Is there anyway for me to add a title to the text file before the data are inputted to the file? if there is a way i will loved to. Thanks your hints have helped me so far.

This question has already been answered. Start a new discussion instead.