English 中文(简体)
具体阐明承认和采掘
原标题:Specific string recognition and extraction

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

页: 1


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 页: 1and 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");

我不想使用PCRE。 我说,它有可能用标准的图书馆,但如何? 从果园中可以如何区分两种界线?

感谢和担忧我的办公室:

最佳回答

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

一旦你打断了两名分离者,终止缓冲,打印,重新制定缓冲指数,然后通过下一名分离者的文件阅读。

这里有一些守则:

#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 ==  
 )) {
                buffer[cIndex] =   ; /* terminate buffer */
                fprintf(stdout, "found number: %d
", atoi(buffer));
                cIndex = 0U;
                continue;
            }
            cIndex++;
        } while (currC != EOF);

        fclose(fp);
    }
    else
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}

请指出以下文件:<代码>编号:txt:

1234
234 567 1
4 5
9

• 编写和管理该守则:

$ 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
问题回答

暂无回答




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签