strCMP与C中的文件不使用同一字符串
因此,我有一个问题,即strcmp
即使字符串相同。目标和源字符串之间的CMP具有相同的字符串,但是当strcmp
IT时,它不起作用。请参阅“说明”代码:
# include <stdio.h>
# include <string.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
char namalengkap[100], gender[100], charnamafile[50], tempat[50], temp[1000], tdeeprint[100], gendercheck;
int umur, tb, bb, x;
int cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, check;
float tdeenum;
fscanf(fp, "Username : %s\nPassword : %s", tempat, temp);
fscanf(fp, "\n\n\n=============================DATA PRIBADI USER===============================\n");
fscanf(fp, "= Nama Lengkap : %52[^\n] =\n", namalengkap);
fscanf(fp, "= Jenis Kelamin : %52[^\n] =\n", gender);
fscanf(fp, "= Umur : %52d =\n", &umur);
fscanf(fp, "= Tinggi Badan (CM) : %52d =\n", &tb);
fscanf(fp, "= Berat Badan (KG) : %52d =\n", &bb);
fscanf(fp, "= Rutinitas Olahraga : %52[^\n] =\n", tdeeprint);
fscanf(fp, "=============================================================================\n");
cmp1 = strcmp(tdeeprint, "Jarang Berolahraga");
cmp2 = strcmp(tdeeprint, "1 Hingga 3 Hari Dalam Seminggu");
cmp3 = strcmp(tdeeprint, "3 Hingga 5 Hari Dalam Seminggu");
cmp4 = strcmp(tdeeprint, "6 Hingga 7 Hari Dalam Seminggu");
cmp5 = strcmp(tdeeprint, "Pekerjaan Fisik Berat Atau Olahraga 2x Dalam Sehari");
if (cmp1 == 0) {
tdeenum = 1.2;
printf("tde1work : %f\n", tdeenum);
} else
if (cmp2 == 0) {
tdeenum = 1.375;
printf("tde2work : %f\n", tdeenum);
} else
if (cmp3 == 0) {
tdeenum = 1.55;
printf("tde3work : %f\n", tdeenum);
} else
if (cmp4 == 0) {
tdeenum = 1.725;
printf("tde4work : %f\n", tdeenum);
} else
if (cmp5 == 0) {
tdeenum = 1.9;
printf("tde5work : %f\n", tdeenum);
}
printf("\nUsername : %s\n", tempat);
printf("\nPassword : %s\n", temp);
printf("\nName : %s\n", namalengkap);
printf("\nGender : %s\n", gender);
printf("\nUmur : %d\n", umur);
printf("\nTB : %d\n", tb);
printf("\nBB : %d\n", bb);
printf("\ntdeeprint : %s\n", tdeeprint);
printf("\ntdeepnum : %f\n", tdeenum);
fclose(fp);
return 0;
}
当我运行它时:
Username : hitunga
Password : 12345
Name : a
Gender : Perempuan
Umur : 30
TB : 155
BB : 50
tdeeprint : Jarang Berolahraga
tdeepnum : 0.000000
Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.
test.txt 文件内容:
Username : hitunga
Password : 12345
=============================DATA PRIBADI USER===============================
= Nama Lengkap : a =
= Jenis Kelamin : Perempuan =
= Umur : 30 =
= Tinggi Badan (CM) : 155 =
= Berat Badan (KG) : 50 =
= Rutinitas Olahraga : Jarang Berolahraga =
= TDEE : 1.200 =
=============================================================================
那么我的代码上有什么问题?我真的很困惑为什么不工作。它实际上是相同的字符串比较,但不起作用。谢谢
编辑: 因此,我用fscanf
替换CMP部分,但仍未得到该号码
fscanf(fp, "= TDEE : %52.3f =\n", &datusr.tdeenum);
So I have a problem that strcmp
not working even the string is same. The cmp between destination and source string is have same string, but when strcmp
it, it not working. See the code for explanation:
# include <stdio.h>
# include <string.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
char namalengkap[100], gender[100], charnamafile[50], tempat[50], temp[1000], tdeeprint[100], gendercheck;
int umur, tb, bb, x;
int cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, check;
float tdeenum;
fscanf(fp, "Username : %s\nPassword : %s", tempat, temp);
fscanf(fp, "\n\n\n=============================DATA PRIBADI USER===============================\n");
fscanf(fp, "= Nama Lengkap : %52[^\n] =\n", namalengkap);
fscanf(fp, "= Jenis Kelamin : %52[^\n] =\n", gender);
fscanf(fp, "= Umur : %52d =\n", &umur);
fscanf(fp, "= Tinggi Badan (CM) : %52d =\n", &tb);
fscanf(fp, "= Berat Badan (KG) : %52d =\n", &bb);
fscanf(fp, "= Rutinitas Olahraga : %52[^\n] =\n", tdeeprint);
fscanf(fp, "=============================================================================\n");
cmp1 = strcmp(tdeeprint, "Jarang Berolahraga");
cmp2 = strcmp(tdeeprint, "1 Hingga 3 Hari Dalam Seminggu");
cmp3 = strcmp(tdeeprint, "3 Hingga 5 Hari Dalam Seminggu");
cmp4 = strcmp(tdeeprint, "6 Hingga 7 Hari Dalam Seminggu");
cmp5 = strcmp(tdeeprint, "Pekerjaan Fisik Berat Atau Olahraga 2x Dalam Sehari");
if (cmp1 == 0) {
tdeenum = 1.2;
printf("tde1work : %f\n", tdeenum);
} else
if (cmp2 == 0) {
tdeenum = 1.375;
printf("tde2work : %f\n", tdeenum);
} else
if (cmp3 == 0) {
tdeenum = 1.55;
printf("tde3work : %f\n", tdeenum);
} else
if (cmp4 == 0) {
tdeenum = 1.725;
printf("tde4work : %f\n", tdeenum);
} else
if (cmp5 == 0) {
tdeenum = 1.9;
printf("tde5work : %f\n", tdeenum);
}
printf("\nUsername : %s\n", tempat);
printf("\nPassword : %s\n", temp);
printf("\nName : %s\n", namalengkap);
printf("\nGender : %s\n", gender);
printf("\nUmur : %d\n", umur);
printf("\nTB : %d\n", tb);
printf("\nBB : %d\n", bb);
printf("\ntdeeprint : %s\n", tdeeprint);
printf("\ntdeepnum : %f\n", tdeenum);
fclose(fp);
return 0;
}
When I run it:
Username : hitunga
Password : 12345
Name : a
Gender : Perempuan
Umur : 30
TB : 155
BB : 50
tdeeprint : Jarang Berolahraga
tdeepnum : 0.000000
Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.
The test.txt file content:
Username : hitunga
Password : 12345
=============================DATA PRIBADI USER===============================
= Nama Lengkap : a =
= Jenis Kelamin : Perempuan =
= Umur : 30 =
= Tinggi Badan (CM) : 155 =
= Berat Badan (KG) : 50 =
= Rutinitas Olahraga : Jarang Berolahraga =
= TDEE : 1.200 =
=============================================================================
So what wrong on my code? I really confused why isn't working. It is literally same string comparison but not working. Thank you
Edit:
So I replace the cmp section with fscanf
, but still not getting the number
fscanf(fp, "= TDEE : %52.3f =\n", &datusr.tdeenum);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将
printf
语句更改为此,则问题可能会变得很清楚:输出摘要:
同样适用于
namalengkap
和gender> gender
。%52 [^\ n]
模式包括所有遵循您想要的空间字符,直到您到达=
,这就是为什么它与不匹配Jarang Berolahraga
。If you change your
printf
statement to this, the problem will likely become clear to you:Output snippet:
The same applies to
namalengkap
andgender
. The%52[^\n]
pattern includes all the space characters that follow what you want until you get to=
, that's why it doesn't match theJarang Berolahraga
.关于最后一个问题,
fscanf(fp,“ = tdee:%52.3f = \ n”,&amp; datusr.tdeenum)
在消耗输入后最多= tdee:因为
%52.3F
不是有效的转换说明符: precision 字段scanf
转换。您应该只使用%52F
或简单地使用%f
。请注意,您的解析器非常脆弱,您不会检查这些
fscanf()
调用的返回值。任何不匹配都会导致输入流与您的代码不同步,然后随后拨打fscanf()
将失败并将目标变量保持不变,因此是非初始化的并打印它们。您应该更改解析方法:一种更安全的方法是一次使用
fgets()
一次读取文件一行,准备解析行的行:删除尾随=
和落后空间,请使用sscanf()
尝试匹配预期数据并检查返回值以检测和报告无效输入。这是一个修改版本:
输出:
Regarding the last question,
fscanf(fp, "= TDEE : %52.3f =\n", &datusr.tdeenum)
fails after consuming the input up to= TDEE :
because%52.3f
is not a valid conversion specifier: there is no precision field inscanf
conversions. You should just use%52f
or simply%f
.Note that your parser is very brittle and you do not check the return values of these
fscanf()
calls. Any mismatch will cause the input stream to get out of sync with your code and subsequent calls tofscanf()
will fail and leave the destination variables unchanged, hence uninitialized, thus causing undefined behavior when you later compare them and print them.You should change your parsing method: a safer approach is to read the file one line at a time with
fgets()
, prepare the line for parsing ie: remove the trailing=
and trailing spaces, usesscanf()
to try and match the expected data and check the return value to detect and report invalid input.Here is a modified version:
Output: