将文件中的数据读入数组 [英] Read data from a file into array

查看:270
本文介绍了将文件中的数据读入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我有2个cvs文件,如下所示:


< spec.csv>

类型,sybType,ID

现金,ON,1

FRA,1x4,2

...


< data.csv>

ID,Rate

1,3

2,5.23

...


我想读取文件并将它们放入2D数组中。我是c ++的新手,请帮我给我一些简单的代码来执行此操作。


以下是我当前的一些代码来读取一个csv文件,但仍然没有成功。

Dear All,

I have 2 cvs file like this:

<spec.csv>
Type,sybType,ID
CASH,ON,1
FRA,1x4,2
...

<data.csv>
ID,Rate
1,3
2,5.23
...

I want to read the files and put them into a 2D array. I am new to c++, pls help to give me some simple code to do this.

The follow are some of my current code to read one csv file, but still not success.

展开 | 选择 | Wrap | 行号

推荐答案

我注意到的一个问题是你正在定义指向字符的指针,但是你没有初始化这些指针的位置。


例如


char * buf


getline(buff,256)

会将文件中的数据写入随机位置 - 谁知道在哪里?


更好的是:


char buf [258]


getline( buff,256)


将你的数据写入编译器为此目的而放置的某个地方。


你们中的一些人指针有同样的问题。


James
A problem that I notice is that you are defining pointers to characters, but you are NOT initializing where those pointers point.

For example

char *buf

getline(buff,256)

will write the data from file to a random location - who knows where?

Better is:

char buf[258]

getline(buff,256)

which will write your data to somewhere that the compiler has laid aside for the purpose.

Some of you oither character pointers have the same problem.

James


如果我想使用指针来访问数组,我需要纠正什么!?它是因为我不想限制数组的大小。谢谢。

What I need to correct if I want to use a pointer to access the array !?It is because I don''t want to limit the size of the array.Thanks.


我注意到的一个问题是你正在定义指向字符的指针,但你不是在初始化那些指针指向的地方。


例如


char * buf


getline(buff,256)

会将文件中的数据写入随机位置 - 谁知道在哪里?


更好的是:


char buf [258]


getline(buff,256)


会写的哟将数据放到编译器为此目的而放置的某个地方。


你们中的一些角色指针都有同样的问题。


James
A problem that I notice is that you are defining pointers to characters, but you are NOT initializing where those pointers point.

For example

char *buf

getline(buff,256)

will write the data from file to a random location - who knows where?

Better is:

char buf[258]

getline(buff,256)

which will write your data to somewhere that the compiler has laid aside for the purpose.

Some of you oither character pointers have the same problem.

James


根据定义,数组的大小有限。


在这种情况下,它没什么大不了的。请注意,在你的getline调用中有256个?你将永远不会从该例程返回超过256个字符。


James
Arrays, by definition, have a limited size.

In this case, it is no big deal. Notice that 256 in your call to getline? You will never get more than 256 characters returned from that routine.

James


这篇关于将文件中的数据读入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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