fread给出空结果,检测到glibc
我想读取一个txt/dat文件,我正在使用以下代码,但它没有加载该文件,因为它为每个指针打印零值fread给出空结果,检测到glibc,c,C,我想读取一个txt/dat文件,我正在使用以下代码,但它没有加载该文件,因为它为每个指针打印零值 #include <stdio.h> #include <stdlib.h> #include <assert.h> int main( int argc, const char* argv[] ){ const int N = 10; double *t = (double*) malloc ( N * sizeof(double) );
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, const char* argv[] ){
const int N = 10;
double *t = (double*) malloc ( N * sizeof(double) );
double *x = (double*) malloc ( N * sizeof(double) );
double *v = (double*) malloc ( N * sizeof(double) );
FILE * theFile;
theFile = fopen( "testFile.dat", "w" );
assert( NULL != theFile );
printf("\n BEFORE \n");
for ( int i = 0; i < N; i++ )
{
t[ i ] = i;
x[ i ] = i + 1;
v[ i ] = i * 2;
// write result to file
fprintf ( theFile, "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
}
fclose( theFile );
// open file for reading
theFile = fopen( "testFile.dat", "r" );
assert( NULL != theFile );
const int buffSize = 3;
double buffer[ buffSize ];
fread( buffer, buffSize , N , theFile );
t = &buffer[ 0 ];
x = &buffer[ 1 ];
v = &buffer[ 2 ];
printf("\n AFTER \n");
for ( int i = 0; i < N; i++ )
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ],x[ i ],v[ i ] );
fclose( theFile );
free ( t );
free ( x );
free ( v );
return 0;
}
#包括
#包括
#包括
int main(int argc,const char*argv[]{
常数int N=10;
double*t=(double*)malloc(N*sizeof(double));
double*x=(double*)malloc(N*sizeof(double));
double*v=(double*)malloc(N*sizeof(double));
文件*文件;
theFile=fopen(“testFile.dat”,“w”);
断言(NULL!=文件);
printf(“\n在\n之前”);
对于(int i=0;i
此外,如果我们有不同的数据类型,例如2个双精度和1个整数,我将使用2个不同的缓冲区并调用fread 2次?您的
fread
调用写入超过缓冲区的末尾:
fread( buffer, buffSize , N , theFile );
如何最好地解决这个问题取决于你到底在做什么。例如,要将三个双精度值读入缓冲区
,应为:
fread( buffer, sizeof(double), buffSize , theFile );
您的fread
调用写入的内容超过了缓冲区的末尾:
fread( buffer, buffSize , N , theFile );
如何最好地解决这个问题取决于你到底在做什么。例如,要将三个双精度值读入缓冲区
,应为:
fread( buffer, sizeof(double), buffSize , theFile );
如果使用fprintf写入文件,则应使用具有相同格式参数的fscanf将其读回。另外,你在阅读部分搞乱了缓冲。我想您的阅读代码应该如下所示(仅做了最基本的修改):
//打开文件进行读取
theFile=fopen(“testFile.dat”,“r”);
断言(NULL!=文件);
常数int buffSize=3;
双缓冲区[buffSize];
对于(int i=0;i
如果使用fprintf写入文件,则应使用具有相同格式参数的fscanf将其读回。另外,你在阅读部分搞乱了缓冲。我想您的阅读代码应该如下所示(仅做了最基本的修改):
//打开文件进行读取
theFile=fopen(“testFile.dat”,“r”);
断言(NULL!=文件);
常数int buffSize=3;
双缓冲区[buffSize];
对于(int i=0;i
您的fprintf()
正在将双精度作为字符串(由制表符分隔)写入文件-这就是转换说明符%5.5f
所做的-它将双精度转换为字符串表示。例如testFile.dat的第一行:
0.00000 1.00000 0.00000
0.00000 1.00000 0.00000
稍后,您将尝试读回双精度,但您将尝试将其视为原始双精度,而不是字符串。您应该能够使用fscanf()
读回双精度值。您的fprintf()
将双精度值作为字符串(由制表符分隔)写入文件-这就是转换说明符%5.5f
所做的-它将双精度值转换为字符串表示。例如testFile.dat的第一行:
0.00000 1.00000 0.00000
0.00000 1.00000 0.00000
稍后,您将尝试读回双精度,但您将尝试将其视为原始双精度,而不是字符串。您应该能够使用fscanf()
读回双精度值。要读回值,您应该将每一行作为字符串读取,并对其进行解析以提取浮点值这是您的代码,已修复
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, const char* argv[] ){
const int N = 10;
double *t = malloc ( N * sizeof(double) );
double *x = malloc ( N * sizeof(double) );
double *v = malloc ( N * sizeof(double) );
FILE * theFile;
theFile = fopen( "testFile.dat", "w" );
assert( NULL != theFile );
printf("\n BEFORE \n");
for ( int i = 0; i < N; i++ )
{
t[ i ] = i;
x[ i ] = i + 1;
v[ i ] = i * 2;
// write result to file
fprintf ( theFile, "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
}
fclose( theFile );
// open file for reading
theFile = fopen( "testFile.dat", "r" );
assert( NULL != theFile );
int i = 0;
while (fscanf(theFile, "%f%f%f", &(t[i]), &(x[i]), &(v[i])) == 3) i++;
printf("\n AFTER \n");
for ( int i = 0; i < N ; i++ )
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ],x[ i ],v[ i ] );
fclose( theFile );
free ( t );
free ( x );
free ( v );
return 0;
}
最后,请务必检查malloc
的返回值,如果出现故障,它将返回NULL
,表明系统内存不足。如果您不检查它,并且它返回NULL
,则在第一次尝试访问它时将出现分段错误
即使这段代码有效,它也存在严重的错误实践问题。我修复了这段代码,使其更加健壮
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, const char* argv[] ){
const int N = 10;
FILE *theFile = NULL;
double *t = NULL;
double *x = NULL;
double *v = NULL;
int errorCode = 0;
t = malloc ( N * sizeof(double) );
if (t == NULL)
goto abort;
x = malloc ( N * sizeof(double) );
if (x == NULL)
goto abort;
v = malloc ( N * sizeof(double) );
if (v == NULL)
goto abort;
theFile = fopen( "testFile.dat", "w" );
if (theFile == NULL)
goto abort;
printf("\n BEFORE \n");
for ( int i = 0; i < N; i++ )
{
t[ i ] = i;
x[ i ] = i + 1;
v[ i ] = i * 2;
// write result to file
fprintf ( theFile, "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ], x[ i ], v[ i ] );
}
fclose( theFile );
// open file for reading
theFile = fopen( "testFile.dat", "r" );
if (theFile == NULL)
goto abort;
int i = 0;
while (i < N) /* stop when you have read enough lines to fit t, x and v */
{
if (fscanf(theFile, "%f%f%f", &(t[i]), &(x[i]), &(v[i])) == 3)
{
/* This will not happen since you just created the file with the appropriate content
* but the good practice here is to check for any problem during the read
*/
errorCode = -1;
printf("malformed line found in file.\n");
goto abort;
}
i++;
}
printf("\n AFTER \n");
for ( int i = 0; i < N ; i++ )
printf( "%5.5f \t %5.5f \t %5.5f \n", t[ i ],x[ i ],v[ i ] );
abort: /* note: This prevents repeating the cleanup code */
if (theFile != NULL)
fclose( theFile );
if (t != NULL)
free ( t );
if (x != NULL)
free ( x );
if (v != NULL)
free ( v );
return errorCode;
}
#包括
#包括
#包括
int main(int argc,const char*argv[]{
常数int N=10;
FILE*theFile=NULL;
double*t=NULL;
double*x=NULL;
double*v=NULL;
int errorCode=0;
t=malloc(N*sizeof(double));
如果(t==NULL)
去流产;
x=malloc(N*sizeof(double));
如果(x==NULL)
去流产;
v=malloc(N*sizeof(双));
如果(v==NULL)
去流产;
theFile=fopen(“testFile.dat”,“w”);
如果(文件==NULL)
去流产;
printf(“\n在\n之前”);
对于(int i=0;i