将数据从文件读入2d数组 [英] reading data from a file into a 2d array

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

问题描述

好的,如果我的第一篇帖子对我来说很容易,而且我已经使用了几个星期的
C.我正在开发一个程序,目前这个项目需要我将.dat文件中的数据读入二维数组然后再打开
打印出二维数组的内容到屏幕。我还需要

将数据附加到.dat文件,但现在大部分时间我都在担心

将信息输入到2d数组中。我的.dat文件看起来像这样


1 20000

2 30000

3 40000


等。第一个代表身份证号码,第二个代表

重量。


#include< stdio.h>

main()

{

FILE * pRead;

int array [20] [3];

pRead = fopen(" test.dat"," r");


if(pRead == NULL)

printf (\ nFile无法打开\ nn;)


其他

printf(&\\; \ n \\ n \\ n的test.dat \ n的内容) \ n");

fscanf(pRead,"%s,%s",数组);


while(!feof(pRead))

{

printf("%s%s \ n",array);

fscanf(pRead,"%s, %s",array);

}


}


我知道我需要使用for循环或者某种想法,以便我可以在列中读取
,然后在行中读取。我真的在这里画了一个空白

如何让它工作。有人能指出我正确的方向吗?

因为现在我的输出是这样的:


11

20002000

22

30003000



此外,如果我使用%d而不是%在这里,我得到了分段错误。可以

任何人解释为什么会发生这种情况?谢谢。

解决方案

你介意告诉我你为什么使用多dimintinal数组吗?对我来说

似乎你也是

使用整数数组并用字符串填充它?


< blockquote> da*********@gmail.com 写道:

你介意告诉我你为什么使用多dimintinal数组吗?对我来说
似乎你也是使用整数数组并用字符串填充它?




请阅读< http:// cfaj .freeshell.org /谷歌/取代。谢谢。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


2006-02-23,yourmycaffiene< dd **** @ gmail.com>写道:

好的,如果我的第一篇帖子对我来说很容易,而且我只用了几个星期就用了。我正在研究一个程序,它目前要求我将.dat文件中的数据读入二维数组,然后将二维数组的内容打印到屏幕上。我还需要
将数据附加到.dat文件中,但现在大部分时间我都在担心将信息输入到2d数组中。我的.dat文件看起来像这样一个20000
2 30000
3 40000

等第一个代表身份证的号码和第二个号码

#include< stdio.h>
main()
{
FILE * pRead;
int数组[20] [3];
pRead = fopen(" test.dat"," r");

if(pRead == NULL)
printf(" ; \ nFile无法打开\ n");

其他
printf(" \\\
Contents of test.dat \ n \ n");
fscanf(pRead,"%s,%s",array);

while(!feof(pRead))
{/> printf("%s%s \\ \\ n,数组);
fscanf(pRead,"%s,%s",数组);
}

}

我知道我需要使用for循环或某些东西来实现这个想法,这样我才能在列中读取然后读取行。我真的在这里画了一个空白
如何让它工作。有人能指出我正确的方向吗?
因为现在我的输出结果是这样的:

11
20002000
22
30003000



另外,如果我在这里使用%d而不是%s,我会遇到分段错误。谁能解释为什么会发生这种情况?谢谢。




查看此代码并从那里开始工作:


#include< stdio.h>

main()

{


unsigned long a,b;


FILE * pRead = fopen(" test.dat"," r");


if(pRead == NULL)

printf(" File无法打开\ n");


其他

printf(" test.dat的内容:\ n");


while(!feof(pRead)){

fscanf(pRead,"%u%u \ n",& a,& b);

printf("%u%u \ nn,a,b);

}


}

一些指示可以帮助你:fcanf可以为你做转换

:如果你使用fscanf就使用它。你正在阅读字符串和

然后将它们存储在int中。存储 - 这是顽皮的,非常好的b $ b不洁净。尝试处理类型并保持它们

一致。虽然C可以并且确实允许您将数据粘贴在错误的

类型内存中。这是一个非常糟糕的主意。


作为练习我们留下的阵列中的存储空间:)


-

删除evomer回复


Okay, this if my first post so go easy on me plus I''ve only been using
C for a couple of weeks. I''m working on a program currently that
requires me to read data from a .dat file into a 2d array and then
print out the contents of the 2d array to the screen. I wil also need
to append data to the .dat file but mostly right now I''m worrying about
getting the info into the 2d array. My .dat file looks like this

1 20000
2 30000
3 40000

etc. The first number standing for an ID and the second standing for a
weight.

#include <stdio.h>
main()
{
FILE *pRead;
int array[20][3];
pRead = fopen("test.dat", "r");

if (pRead==NULL)
printf("\nFile cannot be opened\n");

else
printf("\nContents of test.dat\n\n");
fscanf(pRead,"%s,%s", array);

while ( !feof(pRead) )
{
printf("%s%s\n", array);
fscanf(pRead, "%s,%s", array);
}

}

I know I need to use a for loop or something to that idea so that I can
read in the column and then the row. I''m really drawing a blank here on
how to get that working. Could someone point me in the right direction?
Cause right now my output is coming out like this :

11
20002000
22
30003000

etc.
Also, if I use %d instead of %s here, I get segmentation faults. Can
anyone explain why this is happening? Thanks.

解决方案

do you mind telling me why you used a multi-dimintinal array? to me it
seems also you are
using an integer array and filling it with strings?


da*********@gmail.com writes:

do you mind telling me why you used a multi-dimintinal array? to me it
seems also you are
using an integer array and filling it with strings?



Please read <http://cfaj.freeshell.org/google/>. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


On 2006-02-23, yourmycaffiene <dd****@gmail.com> wrote:

Okay, this if my first post so go easy on me plus I''ve only been using
C for a couple of weeks. I''m working on a program currently that
requires me to read data from a .dat file into a 2d array and then
print out the contents of the 2d array to the screen. I wil also need
to append data to the .dat file but mostly right now I''m worrying about
getting the info into the 2d array. My .dat file looks like this

1 20000
2 30000
3 40000

etc. The first number standing for an ID and the second standing for a
weight.

#include <stdio.h>
main()
{
FILE *pRead;
int array[20][3];
pRead = fopen("test.dat", "r");

if (pRead==NULL)
printf("\nFile cannot be opened\n");

else
printf("\nContents of test.dat\n\n");
fscanf(pRead,"%s,%s", array);

while ( !feof(pRead) )
{
printf("%s%s\n", array);
fscanf(pRead, "%s,%s", array);
}

}

I know I need to use a for loop or something to that idea so that I can
read in the column and then the row. I''m really drawing a blank here on
how to get that working. Could someone point me in the right direction?
Cause right now my output is coming out like this :

11
20002000
22
30003000

etc.
Also, if I use %d instead of %s here, I get segmentation faults. Can
anyone explain why this is happening? Thanks.



Have a look at this code and work from there:

#include <stdio.h>
main()
{

unsigned long a,b;

FILE * pRead = fopen("test.dat", "r");

if (pRead==NULL)
printf("File cannot be opened\n");

else
printf("Contents of test.dat : \n");

while ( !feof(pRead) ){
fscanf(pRead,"%u %u\n", &a,&b);
printf("%u %u\n",a,b);
}

}
Some pointers to help you on the way : fcanf can do the conversions
for you : use it if you use fscanf. You were reading in strings and
then storing them in "int" storage - this is naughty and very
unclean. Try to get a handle on the types and keep them
concistent. While C can and does allow you to stick data in the wrong
"type memory" it is a very bad idea.

The storage in the array we leave as an excercise :)

--
Remove evomer to reply


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

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