从CSV文件中读取值到变量 [英] Reading values from CSV file into variables

查看:243
本文介绍了从CSV文件中读取值到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的一块code的从CSV读取值100条目的最大文件转换成结构的数组。

行的CSV文件的示例:

1,先生,詹姆斯,奎格利,主任,200000,0

我用下面的code中的值阅读,但是当我打印出来的价值观,他们是不正确的。

 为(i = 0;我3;;我++)/ *假设只是这里数项证明问题的* /
    {
    的fscanf(F,%D,%S,%S,%S,%S,%D,%D,&安培; inArray [I] .ID,inArray [I] .salutation,inArray [I] .firstName, inArray [I] .surName,inArray [I] .POSITION,&安培; inArray [I] .sal,&安培; inArray [I] .deleted);
    }

然后,当我打印出的第一个名字,值都分配到的第一个名字:

 为(J = 0; J< 3; J ++)通过打印值/ * *测试/
    {
    的printf(员工姓名为%s \\ n,inArray [J] .firstName);
    }

给出艾姆斯,奎格利,主任,200000,0 等以这种方式。我相信这是我如何格式化的fscanf行,但我不能得到它的工作。

下面是我的结构我读成:

  typedef结构员工
    {
    INT ID;
    焦炭称呼[4];
    焦炭的firstName [21];
    焦姓[31];
    焦位置[16];
    INT萨尔;
    诠释删除;
    }员工;


解决方案

这是因为字符串%S 可以包含逗号,所以它被扫描到的第一个字符串。有没有前瞻中的 scanf()的格式化说明,一个事实,即%S 后跟格式规范字符串逗号意味着什么。

使用字符组(搜索手册获得<大骨节病> [)。

  const int的得到=的fscanf(F,%D,%[^,],%[^,],%[^,],%[^,],%d个%D,&安培; inArray [I] .ID,
                       inArray [I] .salutation,inArray [I] .firstName,
                       inArray [I] .surName,inArray [I] .POSITION,&安培; inArray [I] .sal,
                       &安培; inArray [I] .deleted);

和学习的检查返回值的,因为I / O调用可能会失败!不要依赖于数据是有效的,除非获得 7

为使你的程序读取整个文件(多条记录,即行),我会建议加载整个线成(大)固定大小的缓冲区与fgets(),然后用的sscanf()上缓冲解析出的列值。这是更容易,将确保你确实扫描单独的线路,调用的fscanf()在一个循环不会,因为对的fscanf()换行只是空白。

I am trying to write a simple piece of code to read values from a CSV file with a max of 100 entries into an array of structs.

Example of a line of the CSV file:

1,Mr,James,Quigley,Director,200000,0

I use the following code to read in the values, but when I print out the values they are incorrect

for(i = 0; i < 3; i++) /*just assuming number of entries here to demonstrate problem*/
    {
    fscanf(f, "%d,%s,%s,%s,%s,%d,%d", &inArray[i].ID, inArray[i].salutation, inArray[i].firstName, inArray[i].surName, inArray[i].position, &inArray[i].sal, &inArray[i].deleted);
    } 

Then when I print out the first name, the values are all assigned to the first name:

for(j = 0; j < 3; j++) /* test by printing values*/
    {
    printf("Employee name is %s\n", inArray[j].firstName);
    } 

Gives ames,Quigley,Director,200000,0 and so on in that way. I am sure it's how i format the fscanf line but I can't get it to work.

Here is my struct I'm reading into:

typedef struct Employee
    {
    int ID;
    char salutation[4];
    char firstName[21];
    char surName[31];
    char position[16];
    int sal;
    int deleted;
    } Employee;

解决方案

This is because a string %s can contain the comma, so it gets scanned into the first string. There's no "look-ahead" in the scanf() formatting specifier, the fact that the %s is followed by a comma in the format specification string means nothing.

Use character groups (search the manual for [).

const int got = fscanf(f, "%d,%[^,],%[^,],%[^,],%[^,],%d,%d", &inArray[i].ID,
                       inArray[i].salutation, inArray[i].firstName,
                       inArray[i].surName, inArray[i].position, &inArray[i].sal, 
                       &inArray[i].deleted);

And learn to check the return value, since I/O calls can fail! Don't depend on the data being valid unless got is 7.

To make your program read the entire file (multiple records, i.e. lines), I would recommend loading entire lines into a (large) fixed-size buffer with fgets(), then using sscanf() on that buffer to parse out the column values. That is much easier and will ensure that you really do scan separate lines, calling fscanf() in a loop will not, since to fscanf() a linefeed is just whitespace.

这篇关于从CSV文件中读取值到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆