简体   繁体   中英

How can I use the function sscanf to read specific characters?

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 your zimu 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= or b= , 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.

There is some confusion regarding the %[abc] scansets in scanf format strings: you should specify the maximum number of characters to read and give the character ranges between the [ and ] . There is no trailing s after the ] . Extra spaces in the format string cause spaces to be skipped.

Here is a corrected version of your code.

#include <stdio.h>
#include <string.h>

int main() {
    FILE *fp;
    char buffer[1024];
    char zimu[128];
    char zahla[128];
    char zahlb[128];

    fp = fopen("./0312.conf", "r");
    if (fp == NULL) {
        perror("error");
        return -1;
    }
    zahla[0] = zahlb[0] = '\0';
    while (fgets(buffer, 1024, fp) != NULL) {
        if (sscanf(buffer, " %127[a-z]", zimu) != 1)
            continue;
        printf("zimu is: %s\n", zimu);
        if (strcmp(zimu, "a") == 0) {
            sscanf(buffer, " a = %127[1-9]", zahla);
        }
        if (strcmp(zimu, "b") == 0) {
            sscanf(buffer, " b = %127[1-9]", zahlb);
        }
    }
    printf("a=%s, b=%s\n", zahla, zahlb);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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