可以fscanf()函数读取空白? [英] Can fscanf() read whitespace?

查看:200
本文介绍了可以fscanf()函数读取空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些code。使用读取文本文件的fscanf(),现在我需要修改,​​以便那名$ P $领域pviously空白无必要让空白。该文本文件基本上是形式:

I've already got some code to read a text file using fscanf(), and now I need it modified so that fields that were previously whitespace-free need to allow whitespace. The text file is basically in the form of:

标题:DATA

  标题:DATA

  等等...

title: DATA
title: DATA
etc...

这是基本的解析使用与fgets(inputLine,512,INPUTFILE); sscanf的(inputLine,%* s%S,&安培;数据); ,读取的数据字段,忽略了冠军,但现在的一些数据字段需要允许的空间。我仍然需要忽略的标题和之后立即空白,但随后在包括空白的行的其余部分读取。

which is basically parsed using fgets(inputLine, 512, inputFile); sscanf(inputLine, "%*s %s", &data);, reading the DATA fields and ignoring the titles, but now some of the data fields need to allow spaces. I still need to ignore the title and the whitespace immediately after it, but then read in the rest of the line including the whitespace.

反正是有使用的sscanf()函数来做到这一点?

Is there anyway to do this with the sscanf() function?

如果没有,什么是最小的变化我可以到code,妥善处理空白?

If not, what is the smallest change I can make to the code to handle the whitespace properly?

更新:我编辑的问题,以取代与fgets的fscanf()()+的sscanf(),这是我的code实际使用。我真的不认为这是相关的,当我第一次写这就是为什么我简化它的fscanf()的问题。

UPDATE: I edited the question to replace fscanf() with fgets() + sscanf(), which is what my code is actually using. I didn't really think it was relevant when I first wrote the question which is why I simplified it to fscanf().

推荐答案

如果您不能使用与fgets()使用%[转换说明(与排除选项):

If you cannot use fgets() use the %[ conversion specifier (with the "exclude option"):

char buf[100];
fscanf(stdin, "%*s %99[^\n]", buf);
printf("value read: [%s]\n", buf);

与fgets()是更好的方式。

But fgets() is way better.


编辑:版本与fgets() + 的sscanf()

version with fgets() + sscanf()

char buf[100], title[100];
fgets(buf, sizeof buf, stdin); /* expect string like "title: TITLE WITH SPACES" */
sscanf(buf, "%*s %99[^\n]", title);

这篇关于可以fscanf()函数读取空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆