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
182 views
in Technique[技术] by (71.8m points)

C - format String to array

char str1 [] =
    "{ 'Carl Maria von Weber', 'weber', 8 }{ ' Maria  von   Carl Weber ', 'weber', 1 }
"
    "{ 'Carl Weber Maria von', 'weber', 2 }
"
    "{ 'Carl-Maria von Weber', 'weber', 4 }
"
    "{ 'Chuck Norris', 'norrischuck', 100 }";

If I have such a string, how can I store it in the field, so I should

char field [0] = Carl Maria von Weber
char field [1] = weber
char field [2] = 8

I created the following code, which also stores {}, characters in the field. Can I edit the string str before using the function so that the characters {},? are removed. Or am I wrong about this and do you know any other procedure?

char *src;  
char a[2000];
src=a;

fscanf(fp,"%[^"]",src);
//printf("%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/66068052/c-format-string-to-array

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

1 Reply

0 votes
by (71.8m points)

Since your question didn't contain any requirments for the string, I'm assuming that

  • record format is { 'name1', 'name2', value } (all spaces between parts are important)
  • first field is at most 100 characters and can contain only alphabet characters, spaces or -
  • second field is at most 100 characters and can contain only alphabet characters or spaces
  • third field is a digit.

From your example code I'm assuming also there are at most 1000 records. This code parses and prints your example string:

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

struct parsedStruct
{
  char name1[100];   // Carl Maria von Weber
  char name2[100];   // weber
  int value;         // 8
};

int main (void)
{
  char str1[] =
    "{ 'Carl Maria von Weber', 'weber', 8 }{ ' Maria  von   Carl Weber ', 'weber', 1 }
"
    "{ 'Carl Weber Maria von', 'weber', 2 }
"
    "{ 'Carl-Maria von Weber', 'weber', 4 }
"
    "{ 'Chuck Norris', 'norrischuck', 100 }";

  FILE *fp = fmemopen(str1, sizeof(str1), "r");
  if (fp)
  {
    struct parsedStruct output[1000];
    int numMatched, i = 0;
    do
    {
      numMatched = fscanf(fp, "{ '%100[-a-zA-Z ]', '%100[a-zA-Z ]', %d }", output[i].name1, output[i].name2, &output[i].value);
      if (feof(fp) || ferror(fp))
        break;
      if (numMatched == 3)
        ++i;
      else
        fgetc(fp);
    } while (i < sizeof(output) / sizeof(output[0]));

    fclose(fp);

    for(int j = 0; j < i; ++j)
      printf("%s, %s, %d
", output[j].name1, output[j].name2, output[j].value);
  }
}

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

...