gpt4 book ai didi

C - 从文件中读取字符串。获取随机字符

转载 作者:行者123 更新时间:2023-11-30 17:40:18 25 4
gpt4 key购买 nike

该程序旨在从文件中读取并将读取的内容打印到屏幕上(然后它接受另一个单词,不知道这是否相关)。

FILE *dataMarking;
int x=0, y=0, letters, score;
char scanWord[10][20], currentWord[10][20], scanDescription1[10][40], scanDescription2[10][40], scanDescription3[10][40];

dataMarking = fopen("marking.dat", "r");

if (dataMarking == NULL){page=99;}

else{

x=0;
while (fscanf(dataMarking, "%s:%s:%s:%s\n", scanWord[x], scanDescription1[x], scanDescription2[x], scanDescription3[x]) == 2){x++;};

fclose(dataMarking);

x=0;
while(x<10){
printf("%s\n",scanDescription1[x]);
printf("%s\n",scanDescription2[x]);
printf("%s\n",scanDescription3[x]);
scanf("%s",currentWord[x]);
x++;}

一旦打印到屏幕上,我会得到一系列随机字符,而不是预期的文字。以上是使用的代码;以下是文件中的信息。

one:The number 1:skip:skip
two:The number 2:skip:skip
three:The number 3:skip:skip
four:The number 4:skip:skip
five:The number 5:skip:skip
six:The number 6:skip:skip
seven:The number 7:skip:skip
eight:The number 8:skip:skip
nine:The number 9:skip:skip

提前致谢。

最佳答案

fscanf 返回成功解析的参数数量。fscanf 有四个参数而不是两个。您的循环将在第一次迭代时失败,因为 fscanf 不返回 2。

while (fscanf(dataMarking, "%s:%s:%s:%s\n", scanWord[x], scanDescription1[x], scanDescription2[x], scanDescription3[x]) == 4

关于C - 从文件中读取字符串。获取随机字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com