本文介绍了阅读文件......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我不是C的新手,但是我已经超过5年才使用它了...


我正试图读取一个有两倍的文本文件:


1.0 1.1 1.2 1.3 1.4

2.0 2.1 2.2 2.3 2.4

我'这样做(这只是试图达到目标的一个考验......):


#include< stdio.h>

#include< stdlib.h>


int main(){

FILE * pFile;

long lSize;

double * buffer;


pFile = fopen(" fichero_test.txt"," r");

if(pFile == NULL)退出(1);


//获取文件大小。

fseek(pFile,0,SEEK_END);

lSize = ftell(pFile);

倒带(pFile);


//分配内存以包含整个文件。

buffer =(double *)malloc(lSize);

if(buffer == NULL)exit(2);


//复制文件进入缓冲区。

fread(缓冲区,1,lSize,pFile);

printf(" Pos 2 - %lf \ n",buffer [2]);

printf(" Pos 2 - %lf \ n",buffer [sizeof(double)* 2]);


//终止

fclose(pFile);

免费(缓冲);

返回0;

}


输出结果为:


Pos 2 - 0.000000

Pos 2 - 0.000000


我做错了什么?,我可以将整个文件读入一个缓冲区?

Hi all, I''m not a newbie with C, but I don''t use it since more than 5 years...

I''m trying to read a text file which has doubles in it:

1.0 1.1 1.2 1.3 1.4
2.0 2.1 2.2 2.3 2.4
I''m doing this (it''s only a test trying to achieve the goal...):

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

int main () {
FILE* pFile;
long lSize;
double* buffer;

pFile = fopen ( "fichero_test.txt" , "r" );
if (pFile==NULL) exit (1);

// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

// allocate memory to contain the whole file.
buffer = (double*) malloc (lSize);
if (buffer == NULL) exit (2);

// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
printf("Pos 2 - %lf\n", buffer[2]);
printf("Pos 2 - %lf\n", buffer[sizeof(double)*2]);

// terminate
fclose (pFile);
free (buffer);
return 0;
}

The output is:

Pos 2 - 0.000000
Pos 2 - 0.000000

What am I doing wrong?, Can I read the whole file into a buffer?

推荐答案




是的,但你必须从字符转换为双。最好的

组合是fgets()读取一行,然后是sscanf()来解析

行。

总是检查返回值of sscanf()。


-

Nick Keighley



yes, but you have to convert from characters to double. The best
combination is fgets() to read a line followed by sscanf() to parse the
line.
Always check the return value of sscanf().

--
Nick Keighley




void read_line(double dd [5],FILE * f)
{
if(fscanf(f,"%lf%lf%lf%lf%lf",& dd [0],& dd [1],& dd [2],
& ; dd [3],& dd [4]
!= 5)
{
退出(EXIT_FAILURE);
}
}


void read_line (double dd [5], FILE *f)
{
if (fscanf(f, "%lf %lf %lf %lf %lf", &dd[0], &dd[1], &dd[2],
&dd[3], &dd[4)
!= 5)
{
exit (EXIT_FAILURE);
}
}




好​​吧,它对我不起作用,因为真实文件每行可以有5,6或1000个项目。



Well, it doesn''t works for me because the real file could have 5, 6 or 1000 items per line.



我不确定这是否适用于文本文件...


I''m not certain this works on a text file...




确实...




It does...





ok

你不能把字符填充到一个缓冲区中,并期待理智的结果。


是的,我想是的,但我不确定,这就是我要求帮助的原因....

是的,但你必须从字符转换为double。最好的组合是fgets()读取一行,然后是sscanf()来解析
行。


我认为没有readLine函数,所以我需要阅读,直到我达到''\ n''字符,不是吗? br />
总是检查sscanf()的返回值。



ok
You can''t jsut stuff characters into a buffer and expect sane results.
Yeah, I guess that, but I wasn''t sure of it, that''s the reason I''ve asked for help....
yes, but you have to convert from characters to double. The best
combination is fgets() to read a line followed by sscanf() to parse the
line.
I suppose there isn''t a readLine function, so I''ll need to read until I reach a ''\n'' character, isn''t it?
Always check the return value of sscanf().









这个帖子在''c file reading''帮助吗?



-

===== =========

不是学生

==============



Does this thread on ''c file reading'' help?

http://groups.google.com/group/comp....41f9f1ba8ac3d9
--
==============
Not a pedant
==============


这篇关于阅读文件......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:17