繁体   English   中英

C用换行符解析逗号分隔的值

[英]C parsing a comma-separated-values with line breaks

我有一个包含以下数据的CSV数据文件:

H1,H2,H3
a,"b
c
d",e

当我通过Excel将CSV文件打开时,它可以将工作表的列标题显示为H1, H2, H3 ,列值显示为: a for H1

multi line value as
b
c
d
for H2 

c for H3c for H3我需要使用C程序解析此文件,并像这样拾取值。 但是,我的以下代码段不起作用,因为一列具有多行值:

char buff[200];
char tokens[10][30];
fgets(buff, 200, stdin);
char *ptok = buff; // for iterating
char *pch; 
int i = 0;
while ((pch = strchr(ptok, ',')) != NULL) {
  *pch = 0; 
  strcpy(tokens[i++], ptok);
  ptok = pch+1;
}
strcpy(tokens[i++], ptok);

如何修改此代码段以容纳列的多行值? 请不要被字符串缓冲区的硬编码值所困扰,这是作为POC的测试代码。 从第一原理开始,我想用困难的方式代替任何第三方图书馆。 请帮忙。

精确地解析C语言中“格式正确 ”的CSV的主要复杂之处在于对可变长度字符串和数组的处理,这是通过使用固定长度字符串和数组避免的。 (另一个麻烦是处理格式不正确的CSV。)

没有这些复杂性,解析实际上非常简单:

(未试)

/* Appends a non-quoted field to s and returns the delimiter */
int readSimpleField(struct String* s) {
  for (;;) {
    int ch = getc();
    if (ch == ',' || ch == '\n' || ch == EOF) return ch;
    stringAppend(s, ch);
  }
}

/* Appends a quoted field to s and returns the delimiter.
 * Assumes the open quote has already been read.
 * If the field is not terminated, returns ERROR, which
 * should be a value different from any character or EOF.
 * The delimiter returned is the character after the closing quote
 * (or EOF), which may not be a valid delimiter. Caller should check.
 */
int readQuotedField(struct String* s) {
  for (;;) {
    int ch;
    for (;;) {
      ch = getc();
      if (ch == EOF) return ERROR;
      if (ch == '"') {
        ch = getc();
        if (ch != '"') break;
      }
      stringAppend(s, ch);
    }
  }
}

/* Reads a single field into s and returns the following delimiter,
 * which might be invalid.
 */
int readField(struct String* s) {
  stringClear(s);
  int ch = getc();
  if (ch == '"') return readQuotedField(s);
  if (ch == '\n' || ch == EOF) return ch;
  stringAppend(s, ch);
  return readSimpleField(s);
}

/* Reads a single row into row and returns the following delimiter,
 * which might be invalid.
 */
int readRow(struct Row* row) {
  struct String field = {0};
  rowClear(row);
  /* Make sure there is at least one field */
  int ch = getc();
  if (ch != '\n' && ch != EOF) {
    ungetc(ch, stdin);
    do {
      ch = readField(s);
      rowAppend(row, s);
    } while (ch == ',');
  }
  return ch;
}

/* Reads an entire CSV file into table.
 * Returns true if the parse was successful.
 * If an error is encountered, returns false. If the end-of-file
 * indicator is set, the error was an unterminated quoted field; 
 * otherwise, the next character read will be the one which
 * triggered the error.
 */
bool readCSV(struct Table* table) {
  tableClear(table);
  struct Row row = {0};
  /* Make sure there is at least one row */
  int ch = getc();
  if (ch != EOF) {
    ungetc(ch, stdin);
    do {
      ch = readRow(row);
      tableAppend(table, row);
    } while (ch == '\n');
  }
  return ch == EOF;
}

上面是“从第一原理开始的”-甚至没有使用标准的C库字符串函数。 但是需要花费一些精力来理解和验证。 就个人而言,我会使用(f)lex甚至是yacc / bison(尽管有点过头了)来简化代码并使期望的语法更加明显。 但是在C中处理可变长度结构仍将是第一步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM