【发布时间】:2020-04-02 04:04:51
【问题描述】:
这是正在读取的文件的格式:
键入 扩展名
应用程序/数学 nb ma mb
应用程序/mp21 m21 mp21
我能够从文件中读取每个条目。
现在我想制作一个 key-value 对,其中上述条目的输出类似于
{nb: application/mathematica}
{ma:application/mathematica}
等等。
这是我当前的代码,用于简单地阅读条目
char buf[MAXBUF];
while (fgets(buf, sizeof buf, ptr) != NULL)
{
if(buf[0] == '#' || buf[0] == '\n')
continue; // skip the rest of the loop and continue
printf("%s\n", buf);
}
【问题讨论】:
-
阻止编码的问题是什么?
-
这能回答你的问题吗? store known key/value pairs in c
-
如果您使用
strtok()(或者,更好的是,在 POSIX 上使用strtok_r()或在 Windows 上使用strtok_s()),或者如果您使用sscanf()(请参阅 Using `sscanf() in a loop 以了解有用的信息)信息),然后它们都像跳过单个空白一样愉快地跳过多个空白。像你一样跳过评论和空白行通常是个好主意——做得好。是时候诚实地尝试拆分过程了。
标签: c arrays string key-value fgets