سؤال

i have a file of numbers like that : XXX are unknow numbers

XXXX


YY YYYY YYY YYYY
YYYY YYY YY YYY
ZZZ
UUU UU UUUU UUUUUU UU UUUU
UU UUU UUUU U

the number of numbers per lines and numbers of "line number" are unknowed. I just know how many "blocks" there is. (where a block is a number followed by several number lines)

My aims are: - extracts XXXX and fill a tab with it - tokenize the "line number" into number and file my matrice with it

What i have yet. i read a line, but don't know if it's a single number or a line of numbers.

I tried with sscanf, to determine if there is just one number or several, but it's not conclusive. I checked also the value of ret but sscanf always return the number 1. So it's impossible to determine if there is more than just one number.

ret = sscanf(line, "%d");

I don't want to use PCRE. I'm sur it's possible to make it with the standard c library, but how ? How from a char* can i make the difference between the two kinds of line ?

Thanks, and sorry for my english : )

هل كانت مفيدة؟

المحلول

If your line separator is a newline (\n) and your token separator is a whitespace (\s) then read one character at a time into a buffer.

Once you hit either separator, terminate the buffer, print it, reset the buffer's index, and then keep on reading through the file for the next separator.

Here's some code to do that:

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

/* 
    INT_MAX is 2147483647, and so the maximum digit 
    length is 10. We add another digit to hold a 
    null terminator.
*/

static const unsigned int kMaxNumberLength = 11;
static const char *kNumberFilename = "numbers.txt";

int main(int argc, char *argv[])
{
    FILE *fp = NULL;
    char currC, buffer[kMaxNumberLength];
    unsigned int cIndex = 0U;

    fp = fopen(kNumberFilename, "r");

    if (fp) {
        do {
            currC = fgetc(fp);
            buffer[cIndex] = currC;
            if ((currC == ' ') || (currC == '\n')) {
                buffer[cIndex] = '\0'; /* terminate buffer */
                fprintf(stdout, "found number: %d\n", atoi(buffer));
                cIndex = 0U;
                continue;
            }
            cIndex++;
        } while (currC != EOF);

        fclose(fp);
    }
    else
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}

Let's say you have the following file numbers.txt:

1234
234 567 1
4 5
9

Let's compile and run the code:

$ gcc -Wall test.c
$ ./a.out numbers.txt 
found number: 1234
found number: 234
found number: 567
found number: 1
found number: 4
found number: 5
found number: 9
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top