简体   繁体   English

如何使用scanf读取由/分隔的字符串

[英]How to read string separated by / with scanf

I`ve been trying to this for quite a while now and after some research I had no success, so my last resort was asking a question. 我已经尝试了很长时间了,经过一番研究后我没有成功,所以我最后的办法是提出一个问题。 My input looks like this: 我的输入如下所示:

1.0.0.0/255.0.0.0/127.0.0.1/1112 
1.2.0.0/255.255.0.0/2.4.6.9/1112
1.2.3.0/255.255.255.0/1.2.3.1/111

I need to extract 4 strings from each line, so for exemple the first line would give me 我需要从每一行中提取4个字符串,例如,第一行会给我

s1 = 1.0.0.0
s2 = 255.0.0.0
s3 = 127.0.0.1
s4 = 1112

Here is what I have tried: 这是我尝试过的:

scanf("%s/%s/%s/%s", str1, str2, str3, str4); // This doesn't consume the "/"
scanf("%[^/]s%[^/]s%[^/]s%[^/]s", str1, str2, str3, str4); // This only gets the first string
scanf(""%[^\n]s%*c%s%*c%s%*c%s", str1, str2, str3, str4); // Hera %*c was supposed to consume the "/" and do not store it, it doen't happen tho

How can I get the 4 strings from each input line using a single scanf inside a while (!feof(fileIn)) ? 我如何在while (!feof(fileIn))内使用单个scanf从每个输入行获取4个字符串? Thank you. 谢谢。

There are a few issues with the posted code. 发布的代码存在一些问题。 The scanset directive is %[] ; scanset指令为%[] there is no s in this. 没有s在此。 The format strings using %[^/]s are attempting to match a literal s in the input. 使用%[^/]s的格式字符串正在尝试匹配输入中的文字s But this will always fail because %[^/] matches any character except for / . 但这将始终失败,因为%[^/]匹配/以外的任何字符。 When a / is encountered, the match fails and the / character is left in the input stream. 遇到/时,匹配失败,并且/字符保留在输入流中。 It is this character which must be consumed before continuing on to the next input field. 在继续下一个输入字段之前必须消耗此字符。

Also, note that while(!feof(file)){} is always wrong . 另外, 请注意, while(!feof(file)){}总是错误的 Instead, try fetching input by lines using fgets() , and parsing with sscanf() . 相反,尝试使用fgets()获取输入,并使用sscanf()解析。 The fgets() function returns a null pointer when end-of-file is reached. 到达文件末尾时, fgets()函数将返回空指针。

Further, you should always specify a maximum width when reading strings with scanf() family functions to avoid buffer overflow. 此外,在使用scanf()系列函数读取字符串时,应始终指定最大宽度,以避免缓冲区溢出。

Here is an example program: 这是一个示例程序:

#include <stdio.h>

int main(void)
{
    char input[4096];
    char str1[100];
    char str2[100];
    char str3[100];
    char str4[100];

    while (fgets(input, sizeof input, stdin)) {
        sscanf(input, " %99[^/]/ %99[^/]/ %99[^/]/ %99[^/]",
               str1, str2, str3, str4);

        puts(str1);
        puts(str2);
        puts(str3);
        puts(str4);
    }

    return 0;
}

Sample interaction using sample input from the question: 使用来自问题的样本输入进行样本交互:

λ> ./a.out < readstring_test.txt 
1.0.0.0
255.0.0.0
127.0.0.1
1112 

1.2.0.0
255.255.0.0
2.4.6.9
1112

1.2.3.0
255.255.255.0
1.2.3.1
111

You already got quite close: you missed to consume the delimiter in your second approach: 您已经非常接近:您错过了在第二种方法中使用分隔符的方法:

scanf("%[^/]/%[^/]/%[^/]/%[^/]", str1, str2, str3, str4);

should do the job. 应该做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM