gpt4 book ai didi

c - 我正在尝试从由制表符分隔的文本文件中获取数据。但我无法得到结果

转载 作者:行者123 更新时间:2023-11-30 19:06:56 25 4
gpt4 key购买 nike

这是我的代码

#include <stdio.h>
#include <string.h>

main() {

FILE *fp;
char buff[1024];
char q1[6];
char q2[6];
char * pch;
int i;

fp = fopen("QFile.txt", "r");
fgets(buff, 255, (FILE*)fp);
pch = strtok (buff,"\t");
int count=0;
while (pch != NULL)
{
q1[0]=("%s",*pch);
pch = strtok (NULL, "\t");
}

fgets(buff, 255, (FILE*)fp);
pch = strtok (buff,"\t");
count=0;
while (pch != NULL)
{
q2[0]=("%s",*pch);
pch = strtok (NULL, "\t");
}
for(i=0;i<6;i++)
printf ("%s\n",q1[i]);

for(i=0;i<6;i++)
printf ("%s\n",q2[i]);

fclose(fp);
}

这是我的 QFile.txt 看起来像:

1   20 % of 2 is equal to   1)1 2)0.4   3)0.5   2
2 If Logx (1 / 8) = - 3 / 2, then x is equal to 1)-4 2)4 3)1/4 2

当我编译时,它会显示一些警告:

test3.c:4:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^

test3.c: In function ‘main’:
test3.c:32:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf ("%s\n",q1[i]);
^

test3.c:35:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf ("%s\n",q2[i]);
^

我不知道如何纠正这个问题。我想向两个数组提出两个 mcq 问题。我该如何解决这个问题?

最佳答案

test3.c:4:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^

main() 默认返回 int。写

int main() { 

并返回一个 int, 0 告诉调用者一切正常

   return 0; // final line of main
}

test3.c: In function ‘main’:
test3.c:32:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf ("%s\n",q1[i]);
^

test3.c:35:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf ("%s\n",q2[i]);
^

q1q2 是 char 数组,因此 qx[y] 给出这些数组的第 (y+1) 个元素,即一个字符。

如果您确实想打印位置 i 处的字符,请使用 %c (字符被转换为 int,因此会出现警告 int)

  printf ("%c\n",q1[i]);
printf ("%c\n",q2[i]);

关于c - 我正在尝试从由制表符分隔的文本文件中获取数据。但我无法得到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384375/

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