如何读取数据? [英] How to read data ?

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

问题描述

Hay Guys你们都可以在下面的问题上向我们提出建议


问题:


问题是我有数据有点像这样。


1486,2168,3751,9074,12134,13944,17983,19173,21190,21820,

1730,2640,3450 ,4870,6126,7876,15644,17817,20294,21902,

2070,3025,4333,5854,7805,9231,10597,

16047 .... ....................... soo onnnnnn


现在我存储在ABC.txt文件中的这些数据。


现在我要做的是,我必须从左到右依次从文件

中读取这些数据。


并存入缓冲区。


所以请让我知道如何实现这个。

提前感谢

Ranjeet

解决方案

ra *********** @ gmail.com (ranjeet)写道:

新闻:77 ************* *************@posting.google.c om:

问题:

问题是我的数据是这样的。

1486,2168,3751,9074 ,12134,13944,17983,19173,21190,21820,
1730,2640,3450,4870,6126,7876,15644,17817,20294,21902,
2070,3025,4335,5854,7805 ,9231,10597,
16047 ........................... soo onnnnnn
现在这个数据我已存入ABC.txt文件。

现在我要做的是,我必须从左到右按顺序从文件中读取这些数据。

并存入缓冲区。

所以请让我知道如何实现这一目标。




这应该是可以的fopen()和fread()。您也可以使用

malloc()作为缓冲区。


-

- Mark - >

-


" Mark A. Odell" < OD ******* @ hotmail.com>写道:

ra ********** *@gmail.com (ranjeet)在
新闻中写道:77 ************************** @发布。 google.c om:

1486,2168,3751,9074,12134,13944,17983,19173,21190,21820,
1730,2640,3450,4870,6126 ,7876,15644,17817,20294,21902,
2070,3025,4333,5854,7805,9231,10597,
16047 ................ ........... soo onnnnnn
现在这些数据存储在ABC.txt文件中。

现在我要做的是,我必须从左到右按顺序从文件中读取这些数据。



这应该可以使用fopen()和fread()。




它是文本文件中的文本。我会使用fgets()后跟sscanf(),或者相当

可能(如果我不想用行报告文件格式错误

数字) fscanf()。


Richard


rl *@hoekstra-uitgeverij.nl (Richard Bos)在留言中写道:< 41 **************** @ news.individual.net> ... < blockquote class =post_quotes>" Mark A. Odell" < OD ******* @ hotmail.com>写道:

ra ********** *@gmail.com (ranjeet)在
新闻中写道:77 ************************** @发布。 google.c om:

1486,2168,3751,9074,12134,13944,17983,19173,21190,21820,
1730,2640,3450,4870,6126 ,7876,15644,17817,20294,21902,
2070,3025,4333,5854,7805,9231,10597,
16047 ................ ........... soo onnnnnn
现在这些数据存储在ABC.txt文件中。

现在我要做的是,我必须从左到右按顺序从文件中读取这些数据。
这应该可以使用fopen()和fread()。



它是文本文件中的文本。我会使用fgets()后跟sscanf(),或者非常可能(如果我不想用行
数字报告文件格式错误)fscanf()。




我写了一个简单的程序检查这个。我也在那里使用

将在文本文件的末尾完全停止。


查看程序中的注释。下面!!!


#include< stdlib.h>

#include< stdio.h>

int main() {


FILE * fp;

char buf [6]; //大小为6因为文件有整数

//在文件中只有2个字节(< 32768)


char fch;


if((fp = fopen(" c:\\dummy)。 txt"," r + b"))== NULL){


printf(" \\\
error \ n");

退出(0);

}


while((fch = fgetc(fp))!= EOF){


if(fch =='','')

nofcomma ++; //统计整数的数量

}


arr =(int *)malloc(nofcomma + 2); //分配这么多内存。

倒带(fp); //将指针设置为开头


while((fch = fgetc(fp))!= EOF){


if( fch> 47&& fch< 58)

buf [index ++] = fch;


else if(fch =='',' '|| fch ==''。''){


buf [index] =''\ 0'';

index = 0;

arr [intindex ++] = atoi(buf);

}


}


for(index = 0; index< nofcomma + 1; index ++)

printf(" \ n整数是%d \ n",arr [index]);


返回0;


}


现在这是成功的,因为我能够成功从Text文件中读取整数

(数据)。 (.txt)现在我要做的是我想要/ b
使用存储在数组中的这些数据(arry)再次将它存储到双b / b
中格式。


我正在做的就是使用这个概念。下面

见下面的代码

#include< stdlib.h>

#include< stdio.h>

int main(){


FILE * fp;

char buf [6]; //大小为6因为文件有整数

//在文件中只有2个字节(< 32768)


double * arry_float;


char fch;


if ((fp = fopen(" c:\\dummy.txt"," r + b"))== NULL){


printf(" \ nerror \ n");

退出(0);

}


while((fch = fgetc(fp) )!= EOF){


if(fch =='','')

nofcomma ++; //统计整数的数量

}


arr =(int *)malloc(nofcomma + 2); //分配这么多内存。

arry_float =(double *)malloc(nofcomma + 2);


rewind(fp); //将指针设置为开头


while((fch = fgetc(fp))!= EOF){


if( fch> 47&& fch< 58)

buf [index ++] = fch;


else if(fch =='',' '|| fch ==''。''){


buf [index] =''\ 0'';

index = 0;

arr [intindex ++] = atoi(buf);

arry_float [intindex - 1] =(double)(arr [intindex - 1] / pow(2,13) );

++ intindex;

}


}


for( index = 0; index< nofcomma + 1; index ++)

printf(" \ n整数是%lf \ n",arry_float [index]);

返回0;


}

现在我遇到问题并解决了分段错误

请告诉我什么是我正在做的错误??????

理查德



Hay Guys can you all suggest me the points on the below issue

Problem :

The thing is that I have the data some thing like this.

1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820,
1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902,
2070, 3025, 4333, 5854, 7805, 9231, 10597,
16047........................... soo onnnnnn

now this data I have stored into the ABC.txt file.

Now what I have to do is, that I have to read these data from the file
in sequence from left to right.

And store into the Buffer.

So please let me know how to achive this.
Thank in advance
Ranjeet

解决方案

ra***********@gmail.com (ranjeet) wrote in
news:77**************************@posting.google.c om:

Problem :

The thing is that I have the data some thing like this.

1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820,
1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902,
2070, 3025, 4333, 5854, 7805, 9231, 10597,
16047........................... soo onnnnnn

now this data I have stored into the ABC.txt file.

Now what I have to do is, that I have to read these data from the file
in sequence from left to right.

And store into the Buffer.

So please let me know how to achive this.



This should be possible with fopen() and fread(). You may want to use
malloc() for your buffer too.

--
- Mark ->
--


"Mark A. Odell" <od*******@hotmail.com> wrote:

ra***********@gmail.com (ranjeet) wrote in
news:77**************************@posting.google.c om:

1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820,
1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902,
2070, 3025, 4333, 5854, 7805, 9231, 10597,
16047........................... soo onnnnnn

now this data I have stored into the ABC.txt file.

Now what I have to do is, that I have to read these data from the file
in sequence from left to right.



This should be possible with fopen() and fread().



It''s text in a text file. I''d use fgets() followed by sscanf(), or quite
possibly (if I didn''t want to report file format errors with line
numbers) fscanf().

Richard


rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<41****************@news.individual.net>...

"Mark A. Odell" <od*******@hotmail.com> wrote:

ra***********@gmail.com (ranjeet) wrote in
news:77**************************@posting.google.c om:

1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820,
1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902,
2070, 3025, 4333, 5854, 7805, 9231, 10597,
16047........................... soo onnnnnn

now this data I have stored into the ABC.txt file.

Now what I have to do is, that I have to read these data from the file
in sequence from left to right.
This should be possible with fopen() and fread().



It''s text in a text file. I''d use fgets() followed by sscanf(), or quite
possibly (if I didn''t want to report file format errors with line
numbers) fscanf().



I have written a simple program check this. I am also using that there
will be a FULL STOP at the end of the text file.

SEE the COMMENTs in the program. below !!!

#include<stdlib.h>
#include<stdio.h>
int main() {

FILE *fp;
char buf[6]; // of size size 6 because File has integers
// in the file is only 2 bytes (<32768)

int *arr, nofcomma = 0, index = 0, intindex = 0;
char fch;

if ((fp = fopen("c:\\dummy.txt","r+b")) == NULL) {

printf("\nerror\n");
exit(0);
}

while ((fch = fgetc(fp))!= EOF) {

if (fch == '','')
nofcomma++; // count the no of integers
}

arr = (int *) malloc(nofcomma + 2); //allocate so much of memory.
rewind(fp); // set the pointer aagain to the beginning

while ((fch = fgetc(fp))!= EOF) {

if (fch > 47 && fch < 58)
buf[index++] = fch ;

else if (fch == '','' || fch == ''.'') {

buf[index] = ''\0'';
index = 0;
arr[intindex++] = atoi(buf);
}

}

for ( index = 0; index < nofcomma + 1 ;index++)
printf("\n the integer is %d\n",arr[index]);

return 0;

}

Now what this is working fine as succesfully I am able to read the integer
(data) from the Text file. (.txt) now what I want to do is that I want to
use these data which are stored into the Array (arry) to again store it
into the double format.

what simply i am doing is that i m using this concept. Below
see the below code
#include<stdlib.h>
#include<stdio.h>
int main() {

FILE *fp;
char buf[6]; // of size size 6 because File has integers
// in the file is only 2 bytes (<32768)

int *arr, nofcomma = 0, index = 0, intindex = 0;
double *arry_float;

char fch;

if ((fp = fopen("c:\\dummy.txt","r+b")) == NULL) {

printf("\nerror\n");
exit(0);
}

while ((fch = fgetc(fp))!= EOF) {

if (fch == '','')
nofcomma++; // count the no of integers
}

arr = (int *) malloc(nofcomma + 2); //allocate so much of memory.
arry_float = (double *)malloc(nofcomma + 2);

rewind(fp); // set the pointer aagain to the beginning

while ((fch = fgetc(fp))!= EOF) {

if (fch > 47 && fch < 58)
buf[index++] = fch ;

else if (fch == '','' || fch == ''.'') {

buf[index] = ''\0'';
index = 0;
arr[intindex++] = atoi(buf);
arry_float[intindex - 1] = (double) ( arr[intindex - 1] / pow (2,13));
++intindex;
}

}

for ( index = 0; index < nofcomma + 1 ;index++)
printf("\n the integer is %lf \n", arry_float[index]);
return 0;

}
Now I having the problem and geting the segmentation fault
please let me know what is the fault i am doing ??????


Richard



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

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