跳过TXT文件中的一行返回C中的错误
我的斗争使我不知不觉地弄清楚如何。因此,问题是当我已经扫描第三行时,它给了我一个错误。描述特定问题对我来说有点令人困惑。也许此代码和输出说明可以描述问题。这是代码:
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;
}
因此,此代码部分:
fscanf(fp, "%*[^\n]\n%*[^\n]\n");
fscanf(fp, "%c", check3rd);
如果第三行为空(没有字符或空间),将扫描。如果文件TXT是新的,则将直接针对if部分。像这个文件txt:
1 Username : testa
2 Password : 12345
3
如果我运行它:
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.
之后,这是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
当我使用当前版本的test.txt运行它时,它会给我一个错误:
Process returned -1073741819 (0xC0000005) execution time : 0.548 s
Press any key to continue.
预期输出应该是:
Selamat datang Test A!
那么如何我解决了吗?我是C编程的新手,这非常令人困惑。抱歉,我希望这个问题很容易理解。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是修复整个程序,但是在一行:
您应该通过指针而不是值传递
Check3rd
。这样:
PS。这可能应该是评论,但我不能没有声誉发表评论
This probably doesn't fix whole program, but in line:
you should pass
check3rd
by pointer instead of value.Like this:
PS. This should probably be comment, but I can't post comments without reputation