阅读文件...... [英] Reading a file...
问题描述
大家好,我不是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?
推荐答案
EdUarDo写道:
EdUarDo wrote:
大家好,我是不是C的新手,但我从不使用它超过5年...
你对C i / o的工作方式有一些严重的误解。我推荐
你给自己写了一本好书(例如K& R),它会清楚地解释一下
。
我正试图读取一个有两倍的文本文件:
1.0 1.1 1.2 1.3 1.4
2.0 2.1 2.2 2.3 2.4
说实话,最简单的方法类似于: -
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);
}
}
代码未经测试,未编译等。
我这样做(它只是试图达到目标的测试......):
#include< stdlib.h>
int main (){
int main(无效)
是更好的风格
FILE * pFile;
long lSize;
double * buffer;
pFile = fop en(fichero_test.txt; ,r );
if(pFile == NULL)exit(1);
错误检查,好!
//获取文件大小。
fseek(pFile,0,SEEK_END);
lSize = ftell(pFile);
倒带(pFile);
我不确定这是否适用于文本文件...
//分配内存以包含整个文件。
buffer =(double *)malloc(lSize);
不要施放malloc()
if(buffer == NULL)exit(2);
//将文件复制到缓冲区。
fread(缓冲区,1,lSize,pFile);
printf(Pos 2 - %lf \ n,缓冲区[2]);
所以你已经读了大约40个字符到一个缓冲区。你为什么期望在缓冲区中找到并加倍
?在ascii中,这就像
31 2E 30 20 31 2E 31等
(是的我知道它不会必须是ascii,但我认为这比''''更好地点了
。'''''''''''''''''''''''''''''' 1''''。''''1''等你不同意?)
你不能把字符填充到一个缓冲区中,并期待理智的结果。
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...
you have some severe misconceptions about how C i/o works. I recomend
you get yourself a good book (eg K&R) which will explain things
clearly.
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
to be honest the simplest method is something like:-
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);
}
}
code is untested, uncompiled etc.
I''m doing this (it''s only a test trying to achieve the goal...):
#include <stdio.h>
#include <stdlib.h>
int main () {
int main (void)
is better style
FILE* pFile;
long lSize;
double* buffer;
pFile = fopen ( "fichero_test.txt" , "r" );
if (pFile==NULL) exit (1);
error checking, good!
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
I''m not certain this works on a text file...
// allocate memory to contain the whole file.
buffer = (double*) malloc (lSize);
don''t cast malloc()
if (buffer == NULL) exit (2);
// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
printf("Pos 2 - %lf\n", buffer[2]);
so you''ve read about 40 characters into a buffer. Why do you expect to
find and doubles in the buffer? In ascii that''s something like
31 2E 30 20 31 2E 31 etc.
(yes I know it doesn''t have to be ascii but I thought this made the
point
better than ''1'' ''.'' ''0'' '' '' ''1'' ''.'' ''1'' etc. you disagree?)
You can''t jsut stuff characters into a buffer and expect sane results.
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
>你对C i / o的运作方式有一些严重的误解。我推荐
> you have some severe misconceptions about how C i/o works. I recomend
你给自己写了一本好书(例如K& R),它会清楚地解释一些事情。
当然,现在我已经习惯了其他语言,而且我不记得任何关于CI / O的事情
you get yourself a good book (eg K&R) which will explain things
clearly.
Sure, now I''m used to other languages and indeed I don''t remember anything about C I/O
1.0 1.1 1.2 1.3 1.4
2.0 2.1 2.2 2.3 2.4
1.0 1.1 1.2 1.3 1.4
2.0 2.1 2.2 2.3 2.4
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.
//获取文件大小。
fseek(pFile,0,SEEK_END);
lSize = ftell(pFile);
倒带(pFile);
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
我不确定这是否适用于文本文件...
I''m not certain this works on a text file...
确实...
It does...
//分配内存以包含整个文件。
buffer =(double *)malloc(lSize);
不要投射malloc()
// allocate memory to contain the whole file.
buffer = (double*) malloc (lSize);
don''t cast malloc()
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().
EdUarDo写道:
EdUarDo wrote:
大家好,我不是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)退出(2);
//将文件复制到缓冲区。
fread(buffer,1,lSize,pFile);
printf(" Pos 2 - %lf \\ \\ n \\ n,缓冲区[2]);
printf(Pos 2 - %lf \ n,缓冲区[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?
这个帖子在''c file reading''帮助吗?
http://groups.google.com/group/comp=\"41f9f1ba8ac3d9
-
===== =========
不是学生
==============
Does this thread on ''c file reading'' help?
http://groups.google.com/group/comp....41f9f1ba8ac3d9
--
==============
Not a pedant
==============
这篇关于阅读文件......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!