I have a struggle that somehow I can't figure out how. So the problem is when I already scan the third line, it gives me an error. It's kinda confusing for me to describe the specific problem. Maybe this code and output explanation can describe the problem. Here is the code:
int main() {
FILE *fp;
fp = fopen("test.txt", "a+");
char test = 'T', check3rd, optreg = 'T';
int x;
char namalengkap[50], gender[50];
int umur, tb, bb;
fprintf(fp, "Username : testa\nPassword : 12345\n");
rewind(fp);
fscanf(fp, "%*[^\n]\n%*[^\n]\n");
fscanf(fp, "%c", check3rd);
if (check3rd == '\0') {
printf("Akun baru terdeteksi,Silahkan isi data anda terlebih dahulu\n");
while (optreg !='Y' && optreg !='y') {
fflush(stdin);
printf("Nama Lengkap : ");
fgets(namalengkap, 50, stdin);
strtok(namalengkap, "\n");
printf("Jenis Kelamin : ");
fgets(gender, 50, stdin);
strtok(gender, "\n");
printf("Umur : ");
scanf("%d", &umur);
fflush(stdin);
printf("Tinggi Badan : ");
scanf("%d", &tb);
fflush(stdin);
printf("Berat Badan : ");
scanf("%d", &bb);
fflush(stdin);
printf("Apa anda sudah yakin dengan data ini?\n");
chkch:
printf("(Y/T)");
scanf(" %c", &optreg);
if (optreg != 'Y' && optreg !='y' && optreg != 'T' && optreg != 't') {
puts("input salah\n");
goto chkch;
}
rewind(fp);
fprintf(fp, "\nNama : %s\nJenis Kelamin: %s\nUmur : %d\nTinggi Badan : %d\nBerat Badan : %d",
namalengkap, gender, umur, tb, bb);
}
} else {
printf("Selamat datang %s!\n", namalengkap);
}
return 0;
}
So this code section:
fscanf(fp, "%*[^\n]\n%*[^\n]\n");
fscanf(fp, "%c", check3rd);
Will scan if the third line is empty (no character or space). If the file txt is new then it will be directed to the IF section. Like this file txt:
1 Username : testa
2 Password : 12345
3
If I run it:
Akun baru terdeteksi,Silahkan isi data anda terlebih dahulu
Nama Lengkap : Test A
Jenis Kelamin : Male
Umur : 20
Tinggi Badan : 189
Berat Badan : 87
Apa anda sudah yakin dengan data ini?
(Y/T)Y
Process returned 0 (0x0) execution time : 23.665 s
Press any key to continue.
After that, This is the content of test.txt:
1 Username : testa
2 Password : 12345
3
4 Nama : Test A
5 Jenis Kelamin: Male
6 Umur : 20
7 Tinggi Badan : 187
8 Berat Badan : 80
When I run it with the current version of test.txt, it gives me an error:
Process returned -1073741819 (0xC0000005) execution time : 0.548 s
Press any key to continue.
The expected output should be:
Selamat datang Test A!
So how do I solve it? I'm new to c programming and this is very confusing. Sorry for the long explanation, I hope the question can be easily understood. Thank you.