如何使用函数 sscanf 读取特定字符? [英] How can I use the function sscanf to read specific characters?
问题描述
我想从文件中读取信息,并将文件中a
的值赋给a",将b
的值赋给b".但是没有用.
#include #include #include int main() {文件 *fp;字符缓冲区[1024];查拉[128];字符 zahlb[128];字符子木[128];fp = fopen("./0312.conf", "r");如果(fp == NULL){perror("错误");返回 (-1);}而 (fgets(buffer, 1024, fp) != NULL) {sscanf(buffer, "%^", zimu);printf("zimu 是:%s\n", zimu);如果 (strcmp(zimu, "a") == 0) {sscanf(buffer, "%[1-9]s", zahla);}如果(strcmp(zimu,b")== 0){sscanf(buffer, "%[1-9]s", zahlb);}}printf("a=%s, b=%s\n", zahla, zahlb);}
文件内容为:
a=10b=5
格式说明符
"%^"
没有意义,实际上不会做任何事情——所以你永远不会在zimu
数组中得到任何东西.>格式说明符中
%[1-9]
之后的s
表示匹配文字 's' 字符",因此永远不会匹配.您可能只想删除它.由于您的输入行以
a=
或b=
开头,"%[1-9]"
格式说明符将不匹配.
很可能你想要的是这样的
字符名称[100],值[100];if (sscanf(buffer, " %99[a-z] = %99[0-9]", name, value) == 2) {//匹配一个名称=值的行,用它做一些事情} 别的 {//不匹配——给出错误?}
- 在格式中使用空格字符来跳过空格——只要有空格,它就会跳过 0 个或多个空格字符.请注意 0 - 不需要空格,可以有任意数量的空格
- 始终检查返回值以查看预期的项目数是否匹配
- 您需要某种格式来匹配该行上的所有字符,但除了
%[
和%c
之外的格式说明符也将跳过空格,您可以如果您不关心尾随的东西,请忽略它.如果您确实关心尾随的内容,您可以使用%n
来检查您扫描了多少个字符以确保它是整行. - 在读取固定数组的
%s
和%[
说明符上使用边界以确保它们不会溢出.
I want to read the information from the file and give the value of a
in file to the "a" and value of b
to the "b". But it didn't work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *fp;
char buffer[1024];
char zahla[128];
char zahlb[128];
char zimu[128];
fp = fopen("./0312.conf", "r");
if (fp == NULL) {
perror("error");
return (-1);
}
while (fgets(buffer, 1024, fp) != NULL) {
sscanf(buffer, "%^", zimu);
printf("zimu is: %s\n", zimu);
if (strcmp(zimu, "a") == 0) {
sscanf(buffer, "%[1-9]s", zahla);
}
if (strcmp(zimu, "b") == 0) {
sscanf(buffer, "%[1-9]s", zahlb);
}
}
printf("a=%s, b=%s\n", zahla, zahlb);
}
the content of the file is:
a=10 b=5
The format specifier
"%^"
is meaningless and won't actually do anything -- so you never get anything in yourzimu
array.The
s
after%[1-9]
in a format specifier means "match a literal 's' character", so will never match. You probably just want to remove it.Since your input lines start with
a=
orb=
, the"%[1-9]"
format specifiers will not match.
Most likely what you want is something like
char name[100], value[100];
if (sscanf(buffer, " %99[a-z] = %99[0-9]", name, value) == 2) {
// matched a line with name=value, do something with it
} else {
// didn't match -- give an error?
}
- use space characters in the format to skip whitespace -- whenever there is a space it will skip over 0 or more whitespace characters. Note the 0 -- a space is not required, and any number of spaces may be there
- ALWAYS check the return value to see if the expected number of items matched
- You need something in the format to match all the characters on the line, though format specifiers other than
%[
and%c
will also skip whitespace, and you can ignore trailing stuff if you don't care about it. If you do care about trailing stuff, you can use%n
to check how many characters you scanned to make sure it is the entire line. - use bounds on
%s
and%[
specifiers reading into fixed arrays to ensure that they don't overflow.
这篇关于如何使用函数 sscanf 读取特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!