繁体   English   中英

使用scanf将csv文件读取到C中的结构中

[英]Reading a csv file into a struct in C using scanf

我在从csv数据文件读入C中的二进制搜索树的节点时遇到问题。似乎没有任何数据实际上在读入结构。 我现在使用的代码只是试图从csv文件中读取一行数据,然后才能进行扩展以读取整个内容,但是即使如此,我也没有得到结果。 我知道此代码中可能存在很多大问题,因为我对这种语言不是很称职,但是请您多加理解。

typedef struct{
  struct bst_t *left;
  struct bst_t *right;
  data_t data;
} bst_t;

这是我的阅读功能

void readdata(bst_t node){
  while(
  scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],
   %[^,],%[^,],%[^,],%[^,] ,[^,],%[^,],%[^,] ... ) == 14);
}

这是我的打印功能

void printdata(bst_t node){
  printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s \n"...);
}

但是我的输出很简单:

-bash-4.1$ print

,,,,,,,,,,.,(@,,▒

我还要面对的另一个问题是,文件中的某些数据在条目中将包含逗号,我将如何“忽略”这些逗号,使它们作为数据而不是文件中的分隔符出现?

再次,任何帮助将不胜感激。

编辑:这就是我所谓的功能:(即主要)

int main(int argc, char *argv[]) {
bst_t node;
readdata(*node);
printdata(node);
return 0;

}

新的编译器代码

print.c: In function 'main':
print.c:37: error: invalid type argument of 'unary *' (have 'bst_t')
print.c: In function 'readdata':
print.c:56: error: request for member 'node' in something not a structure     or union

这是完整的代码:

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


#define MAXSTRING 128


typedef struct{
  struct bst_t *left;
  struct bst_t *right;
  struct data_t data;
} bst_t;

void readdata(bst_t *node);
void printdata(bst_t node);

int main(int argc, char *argv[]) {
    bst_t node;
    readdata(&node);
    printdata(node);
    return 0;
}


void readdata(bst_t *node){
  while(
  scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,] \n",...) == 14)

}

读取函数仅更新它通过值作为参数接收的局部变量node 您必须将指针传递给新分配的结构:

void readdata(bst_t *node) {
    while (scanf(" %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^,],%[^,],%[^,]",
                 node->data.ID, node->data.name, node->data.sex,
                 node->data.height, node->data.weight, node->data.team,
                 node->data.NOC, node->data.games, node->data.year,
                 node->data.season, node->data.city, node->data.sport,
                 node->data.event, node->data.medal) == 14) {
        continue;
    }
} 

您可以通过以下方式从main调用此函数:

int main(int argc, char *argv[]) {
    bst_t node;
    readdata(&node);
    printdata(node);
    return 0;
}

但是请注意,此功能不安全:它无法防止缓冲区溢出。

还要注意,它不能处理空字段,也不能处理带有嵌入式逗号的字段。

为了正确地解析输入,您需要一个处理特殊情况并提供精确错误报告的手动编码解析器。

编辑:您发布的源代码有语法错误:

               node->data.event->node.data.medal) == 14)

它应显示为:

               node->data.event, node->data.medal) == 14)
        continue;

你应该更可读取格式化你的代码,由4个空格缩进语句和周围插入二元运算符和后位,

尝试使用此版本的readdata :(从网页复制并粘贴)

void readdata(bst_t *node) {
    while (scanf(" %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^,],%[^,],%[^,]",
                 node->data.ID, node->data.name, node->data.sex,
                 node->data.height, node->data.weight, node->data.team,
                 node->data.NOC, node->data.games, node->data.year,
                 node->data.season, node->data.city, node->data.sport,
                 node->data.event, node->data.medal) == 14) {
        continue;
    }
} 

暂无
暂无

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

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