Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

C formating String

I have functin and input like this

"{ 'Carl Weber Maria von', 'weber', 2 }
"
"{ 'Carl-Maria von Weber', 'weber', 4 }
"
"{ 'Chuck Norris', 'norrischuck', 100 }";

It creates outputi like this:

Chuck Norris
,
norrischuck
,
100
}{

How can I make sure that no characters like {}, are stored in my field.Somehow I can't modify this function to achieve the desired result. Thank you in advance for the answers. Function:

int fillPoints  (FILE *fp, TSTUDENTLIST *l)
{
    char *src;  
        char a[2000];
        src=a;
        
        fscanf(fp,"%[^"]s",src);
    
        int count=1000;
        char output[1000][1000];    
        int i = 0;
        while (i < count) {
            const char *start;
            int len;
            while (isspace((unsigned char)*src))
                src++;
            if (*src == '')
                break;
            if (*src == ''') {
                start = ++src;
                len = strcspn(src, "'");
                src += len;
                if (*src == ''')
                    src++;
            } else
            if (*src == '"') {
                start = ++src;
                len = strcspn(src, """);
                src += len;
                if (*src == '"')
                    src++;
            } else {
                start = src;
                len = strcspn(src, " fv
");
                src += len;
            }
            snprintf(output[i], sizeof(output[i]), "%.*s", len, start);
            i++;
        }
}
question from:https://stackoverflow.com/questions/66066530/c-formating-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For simplicity I suggest you do it in multiple passes over each line, where each pass copies part of the line into temporary array.

For example an initial pass to remove a trailing semi-colon if one exists (for this you don't actually need to copy). Then a pass to copy all but the opening and closing double-quotes ". Then one pass for the braces. And one pass for the back-slashes (and the n in ). And a last pass for the single quotes.

All that should leave you with something like "Chuck Norris, norrischuck, 100". And this can be fed to strtok to "tokenize" on the comma, and you simply call it twice to get the three separate strings "Chuck Norris", "norrischuck", and "100". The last you could pass to strtoul to convert to an integer.

You can of course combine all the passes into a single pass once you get the long multi-pass solution working.


When I say you make a "pass" over the input, I mean you iterate over the string, copying all but the unwanted characters to a new temporary array.

For example:

// Previous pass puts its output in pass_1_output

// Pass to remove double-quotes
char pass_2_ouput[1000] = { 0 };  // Zero-initialize, which is the string terminator

for (size_t in = 0, out = 0; pass_1_output[in] != ''; ++in)
{
    if (pass_1_input[in] != '"')
    {
        // Not a double-quote, copy the input to the output
        pass_2_output[out++] = pass_1_input[i];
    }
}

// After the above loop, pass_2_output will contain the same contents as
// pass_1_output, *except* any double-quotes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...