C语言 使用 fscanf 读取行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18098564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:05:25  来源:igfitidea点击:

reading lines using fscanf

cstdscanf

提问by KAKAK

hi i have a file which contains following lines:

嗨,我有一个包含以下几行的文件:

wwa weweweof ewewe wdw:

      1    11 ms    <1 ms    <1 ms  174.78.134.1  
      2    11 ms    <1 ms    <1 ms  174.78.134.1 
      3     5 ms    <1 ms    <1 ms  x58trxd00.abcd.edu.com [143.71.290.42] 
      4    11 ms    <1 ms    <1 ms  174.78.134.1 

i am using

我在用

    if(linecount == 8)
    while( fscanf(fp, "%d %s %s %s %s  %s %s",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8) != EOF ){  
          printf("%s",ch);
        } 

    else if (linecount == 9){
       while( fscanf(fp, "%d %s %s %s %s  %s %s %s",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9) != EOF ){  
          printf("%s",ch);
       }
    }

how do i check if the line rows in the file contain 8 or 9 elements, so that i can run the above else-if statements accordingly?

我如何检查文件中的行行是否包含8 个或 9 个元素,以便我可以相应地运行上述 else-if 语句?

回答by

reading lines using fscanf

使用 fscanf 读取行

No.

不。

But I though it was a good idea because...

但我认为这是个好主意,因为...

No.

不。



Reading lines is done using the fgets()function (read the funny manual):

阅读行是使用fgets()功能完成的(阅读有趣的手册):

char buf[LINE_MAX];
while (fgets(buf, sizeof buf, file) != NULL) {
    // process line here
}

Then, you can parse it using sscanf()(not preferred) or sane functions like strchr()and strtok_r()(preferred). It is also worth not(h)ing that the return value of scanf()(docs) is notEOF, but the number of entities successfully read. So, a lazy approach for parsing the string may be:

然后,您可以使用sscanf()(非首选)或像strchr()strtok_r()(首选)这样的理智函数来解析它。同样值得一提的是scanf()( docs)的返回值不是EOF,而是成功读取的实体数量。因此,解析字符串的懒惰方法可能是:

if (sscanf(buf, "%s %s %s %s %s %s %s %s %s", s1, s2, ...) < 9) {
    // there weren't 9 items to convert, so try to read 8 of them only
}

Also note that you better use some length-limitation with the %sconversion specifier in order to avoid buffer overflow errors, like this:

另请注意,您最好在%s转换说明符中使用一些长度限制,以避免缓冲区溢出错误,如下所示:

char s1[100];
sscanf(buf, "%99s", s1);

Likewise, you should not use the &address-of operator when scanning (into) a string - the array of char already decays into char *, and that is exactly what the %sconversion specifier expects - the type of &s1is char (*)[N]if s1is an array of N chars - and a mismatching type makes scanf()invoke undefined behavior.

同样,&在扫描(进入)字符串时不应使用address-of 运算符 - char 数组已经衰减为char *,这正是%s转换说明符所期望的 - if的类型&s1是N s的数组- 和不匹配的类型会导致调用未定义的行为。char (*)[N]s1charscanf()

回答by Some programmer dude

Use fgetsto get the line, and then do one sscanfto check for 9 elements, and if that fails then use sscanfagain to check for the 8 element line.

使用fgets获取行,然后执行一次sscanf检查 9 个元素,如果失败则sscanf再次使用检查 8 个元素的行。

If the format of the line is equal except one extra item, then remember that the scanffamily of functions return the number of successfully scanned items. So it might be enough to just call sscanfonce, and check if it returns 8or 9.

如果行的格式相同,除了一个额外的项目,那么请记住scanf函数系列返回成功扫描项目的数量。因此,只需调用sscanf一次,然后检查它是否返回89.

回答by swagger

You have to use the fgets()function because your swagvalue in your array is currently set to null.

您必须使用该fgets()函数,因为您swag的数组中的值当前设置为null.

回答by The_dempa

and don't use &with stringtype (char arrays) data types with scanf

并且不要&string类型(字符数组)数据类型一起使用scanf

scanf("%s",name); //use it without "&" sign

reason:

原因:

A "string" in C is the address of a character buffer. You want scanf to fill the memory in the buffer, which is pointed to by the variable.

C 中的“字符串”是字符缓冲区的地址。您希望 scanf 填充变量指向的缓冲区中的内存。

In contrast, an int is a block of memory, not an address. In order for scanf to fill that memory, you need to pass its address.

相反, int 是一块内存,而不是地址。为了使 scanf 填充该内存,您需要传递其地址。