gpt4 book ai didi

c - 使用 fscanf 获取 2 列中的 2 个字符串

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

我试图让我的程序读取一个包含 2 列的文件,第一列包含一些字符串,但我无法将其存储到数组中

这是我的代码:

fp2=fopen("Symbol Table.txt","r");char str[100];
while(fscanf(fp2,"%s %s",str,stemp[scnt])!=NULL) {
puts(stemp[scnt++]);getch(); //This is just here to display conents of second col
}
fclose(fp2)

这是我的 txt 文件:

void                                                          void                                                          
main Main
( Left Parenthesis
) Right Parenthesis
{ Left Brace
S Identifier
: Colon
$% Start of Block Comment
This program is a simple calculatorFuctions:ADD,SUB,MULT,DIV String
%$ End of Block Comment
Unsigned Noise Words
int Integer

代码存储长字符串在进入数组之前被分割

最佳答案

当您尝试使用 scanffscanf 读取字符串时,它会读取字符串,直到遇到空格、制表符或换行符。在这种情况下,正如我所看到的,在第一个循环中, strstemp 将被分配给字符串“void”和“void”,在第二个循环中,它将是“main”和“Main”,在第三个循环中,“(”和“Left”等等。

您需要使用制表符 (\t) 等特殊字符分隔具有多个字符串的两列,以便知道第一列何时结束并使用 getc 函数而不是 scanf ,直到到达行中的特殊字符,然后将所有字符(即字母)组合成一个字符串。您可以采用相同的方法使用 getc 函数读取第二列,直到遇到换行符 (\n)。

关于c - 使用 fscanf 获取 2 列中的 2 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877280/

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