c/c++语言开发共享区分文件C中的数据

我试图读取文件的内容,这些文件被分成两个不同的类型。 如下所示:

# Type names bird mammal reptile . # Type effectiveness Very_effective Not_effective . 

到目前为止,我可以读取第一种类型的内容,但是当我尝试读取第二种内容时,我不断重读第一种内容。

 #include  #include  #include  int main() { typedef struct { char types[1000]; char effectiveness[1000]; } sinFile; sinFile record[1000]; FILE* file; char line[121]; char period[10]; char hash[10]; char* item; char* item2; int i = 0; int j = 0; file = fopen("Test.txt", "r"); while(fgets(line, 120, file)) { item = strtok(line, " "); strcpy(period, "."); if (item[0] == '#') { continue; } else { do { strcpy(record[i].types, line); i++; } while (strcmp(record[i].types, period) == 0); } item2 = strtok(line, " "); if (item2[0] == '#') { continue; } else { do { strcpy(record[j].effectiveness, line); j++; } while (strcmp(record[j].effectiveness, period)== 0); } } fclose(file); printf("%s", record[0].effectiveness); } 

在我尝试打印出第一个有效类型的那一刻,它打印出“鸟”等。

我觉得我很接近,但我不确定如何处理。

    问题在于你使用strtok() 。 从联机帮助页:

    strtok()函数将字符串解析为一系列标记。 在第一次调用strtok()时,应该在str中指定要解析的字符串。 在应该解析相同字符串的每个后续调用中,str应为NULL。

    这意味着要解析str ,你必须首先将其称为strtok(str, " ") ,并进一步解析字符串,你必须将其称为strtok(NULL, " ") 。 当你第二次称它为strtok(str, " ")时,它会从str的开头再次开始。

      以上就是c/c++开发分享区分文件C中的数据相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519783.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐