#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;
}
}
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