按线路读取线和匹配模式-C

发布于 2025-01-18 22:06:47 字数 375 浏览 1 评论 0原文

我有一个文本文件,其中包含如下行:

“字符串可能包含多个单词 - 字符串与前一个字符串相同 - 数量”

我用 fgets 逐行读取该文件,但 sscanf 只是切断了除第一个字符串之外的所有内容。 我该如何解决这个问题?

char new_name[30], new_district[30], buffer[100];
unsigned new_part_count;
while(fgets(buffer, 100, file)) {
    sscanf(buffer, "%s - %s - %u", new_name, new_district, &new_part_count);
}

I have a text file with lines like this:

"string maybe contains more than one word - string as the previous -
number"

I read from this file line by line with fgets, but the sscanf just cut off everything except the first string.
How can I solve this problem?

char new_name[30], new_district[30], buffer[100];
unsigned new_part_count;
while(fgets(buffer, 100, file)) {
    sscanf(buffer, "%s - %s - %u", new_name, new_district, &new_part_count);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2025-01-25 22:06:47

尝试以下调用,

sscanf( buffer, "%29[^-] - %29[^-] - %u", new_name, new_district, &new_part_count );
    

如果仅使用转换说明器%s,则任何白空间字符终止字符串的输入。

Try the following call

sscanf( buffer, "%29[^-] - %29[^-] - %u", new_name, new_district, &new_part_count );
    

If to use just the conversion specifier %s then any white space character terminates the input of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文