Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STRING_LEN 200

int new_acc(FILE * fw, char *name, int *dob, int *id);
int i;
char one='1', two='2', choice[STRING_LEN], option;
static const char * listing[] = {"Name", "Date of birth","ID card number"};

int main(){
	FILE * fw = fopen("fiile.csv","a");
	int dob[STRING_LEN], id[STRING_LEN];
	char name[STRING_LEN];

do{
	while(two != choice[0]){
		printf("%c. Create new account\n",one);
		printf("%c. Update information\n",two);

		printf("Enter your choice: ");
		fgets(choice, STRING_LEN, stdin);          

		if (choice[0] == one){
			new_acc(fw, name, dob, id);
			break;
		}
		else if (choice[0] == two){
			printf("Selected Two\n");
			break;
		}
		else{
			printf("Please enter the correct information.\n");
			break;
		}
	} // End of while loop

	printf("Go to the main menu [y/n]: ");
	scanf("%s", &option);
	while (getchar() != '\n') continue;
  }
  while (option == 'y');

  return 0;
}

int new_acc(FILE * fw, char *name, int *dob, int *id){
	char data[8][STRING_LEN];
  do{
    for (i = 0; i < 3; i++) {
      printf("Enter your %s: ", listing[i]);
      fgets(data[i], STRING_LEN, stdin);
      while(strcmp(data[i], "\n") == 0){
        fgets(data[i], STRING_LEN, stdin);
      }
      data[i][strlen(data[i])-1] = '\0';
    }

    fprintf(fw, "%s,%s,%s\n", data[0], data[1], data[2]);
    printf("Insert Data Again [y/n]: ");
  	scanf("%s", &option);
  }
  while(option == 'y');

  fclose(fw);
return 0;
}


What I have tried:

In this code whenever I try to create a new account and enter information. In the end, it gives me the option to Go to the main menu. I press y to continue and try to create new account again. This time the data does not write in the file. Whenever I exit the program and start again to write, it works. But the problem is not in writing. Instead in the same running program, it does not write the data into a file
Posted
Updated 7-Sep-20 20:05pm
v2

1 solution

If you look at the end of your new_acc function, you'll see why:
C++
fclose(fw);

Since the file is closed after the first write and only gets opened at the start of main the second and subsequent writes will always fail.
 
Share this answer
 
Comments
ibilalkayy 7-Sep-20 16:14pm    
Thanks bro
OriginalGriff 7-Sep-20 17:14pm    
You're welcome!
CPallini 8-Sep-20 2:15am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900