简体   繁体   中英

Sscanf in C - reading numbers from a .txt. file

I have written a program, which is reading all the numbers from a .txt file - it is working fine. However, I would like to use only one sscanf function in my program reading all the numbers instead of using isdigit(*poi) and sscanf again.

If I don't use isdigit(*poi) , I will not get the numbers, which would be at the beginning of a line in the .txt file.

How can I tell sscanf to keep reading even if there is no string to match?

Code:

int test(FILE *file, FILE *file2){
    int l = 0;
    int nc = 0;
    char buffer[1000];
    char *poi = NULL;
    fprintf(file2, "\n");
    while(fgets(buffer, 1000, file) != NULL){
        poi = buffer;
        while(*poi){
            if(isdigit(*poi)){
                sscanf(poi, "%d%n",&zahl[l].number, &nc);
                poi += nc;
                fprintf(file2, "%d ", zahl[l].number);
            }
            if(1 == sscanf(poi, "%*[^0-9]%d%n",&zahl[l].number, &nc)){
                poi += nc;
                fprintf(file2, "%d ", zahl[l].number);
            }
            else{
                break;
            }
            l++;

        }
        fprintf(file2, "\n");
    }
    return 0;
}

Sample text in .txt :

text 2025 text text 25 text text text 1h text
26 text text text text 4,5h.
text text text text 19h

Output should be:

2025 25 1
26 4 5 
19

Appending of would be fine:

    if(1 == sscanf(poi, "%*[^0-9]%d%n",&zahl[l].number, &nc)){
            poi += nc;
            fprintf(file2, "%d ", zahl[l].number);
        }

Instead of using :

        if(isdigit(*poi)){
            sscanf(poi, "%d%n",&zahl[l].number, &nc);
            poi += nc;
            fprintf(file2, "%d ", zahl[l].number);
        }

I would like to use only one sscanf function in my program

Use the return value of sscanf() . When it is not 1, increment poi and re-scan at the next character.

while(fgets(buffer, 1000, file) != NULL){
  poi = buffer;
  while(*poi){
    int nc = 0; 
    int cnt = sscanf(poi, "%d%n", &zahl[l].number, &nc);
    if (cnt == 1) {
      poi += nc;
      fprintf(file2, "%d ", zahl[l].number);
    } else {
      poi++;
    }
  }
  fprintf(file2, "\n");
}

make %[^0-9]%n a separate sscanf . Set nc to zero and if this sscanf fails, zero will be added to offset .
sscanf for a integer and if that fails, break out of the loop.

#include <stdio.h>

int main ( void) {
    char *poi = NULL;
    char *lines[] = {
    "text 2025 text text 25 text text text 1h text"
    , "26 text text text text 4,5h."
    , "text text text text 19h"};
    int value = 0;
    int nc = 0;
    for ( int each = 0; each < 3; ++each) {
        poi = lines[each];
        while ( 1) {
            nc = 0;
            sscanf ( poi, "%*[^0-9]%n", &nc);
            poi += nc;
            if ( 1 == sscanf ( poi, "%d%n", &value, &nc)) {
                printf ( "%d ", value);
                poi += nc;
            }
            else {
                break;
            }
        }
        printf ( "\n");
    }
    return 0;
}

to handle - numbers.

#include <stdio.h>

int main ( void) {
    char *poi = NULL;
    char *lines[] = {
    "text 2025 text text 25 text text text 1h text"
    , "26 text text text text 4,-5h."
    , "text text text text 19h"};
    int value = 0;
    int nc = 0;
    for ( int each = 0; each < 3; ++each) {
        poi = lines[each];
        while ( 1) {
            nc = 0;
            sscanf ( poi, "%*[^0-9]%n", &nc);
            poi += nc;
            if ( poi > lines[each]) {
                if ( '-' == *(poi - 1)) {
                    --poi;
                }
            }
            if ( 1 == sscanf ( poi, "%d%n", &value, &nc)) {
                printf ( "%d ", value);
                poi += nc;
            }
            else {
                break;
            }
        }
        printf ( "\n");
    }
    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