如何在c程序中从命令行打开和读取二进制文件
我试图用C编写一个代码,用linux从命令行读取二进制文件。读取文件的规范如下: *输入文件的名称将作为命令行参数传递到程序中 *程序将打开这个二进制文件并读取文件中的第一个整数。然后,它将使用malloc函数动态创建一个这个大小的浮点数组 *然后,程序将读取浮点值并将它们存储到这个新装箱的数组中 我唯一能成功完成的事情就是打开文件如何在c程序中从命令行打开和读取二进制文件,c,linux,ubuntu,command-line,binary,C,Linux,Ubuntu,Command Line,Binary,我试图用C编写一个代码,用linux从命令行读取二进制文件。读取文件的规范如下: *输入文件的名称将作为命令行参数传递到程序中 *程序将打开这个二进制文件并读取文件中的第一个整数。然后,它将使用malloc函数动态创建一个这个大小的浮点数组 *然后,程序将读取浮点值并将它们存储到这个新装箱的数组中 我唯一能成功完成的事情就是打开文件 file=double*malloc(30*sizeof(double)); 但是,当我试图将文件放入fread函数的*ptr参数时,不断出现错误并遇到一些严重问
file=double*malloc(30*sizeof(double));
但是,当我试图将文件放入fread函数的*ptr参数时,不断出现错误并遇到一些严重问题
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
这就是我到目前为止所做的:
#include <stdio.h>
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
{
printf( "usage: %s filename", argv[0] );
}
else
{
FILE *file = fopen( argv[1], "r" );
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
int x;
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
fclose( file );
}
}
}
#包括
int main(int argc,char*argv[])
{
如果(argc!=2)
{
printf(“用法:%s文件名”,argv[0]);
}
其他的
{
FILE*FILE=fopen(argv[1],“r”);
如果(文件==0)
{
printf(“无法打开文件\n”);
}
其他的
{
int x;
而((x=fgetc(文件))!=EOF)
{
printf(“%c”,x);
}
fclose(文件);
}
}
}
也许是这样的
int size;
float *floatArray;
FILE *file = fopen( argv[1], "r" );
fread(&size, sizeof(int), 1, file);
floatArray = (float*) malloc(sizeof(float) * size);
fread(floatArray, sizeof(float), size, file);
for(int i = 0; i < size; i++)
{
printf("%d: %f\n", i+1, floatArray[i]);
}
int大小;
浮点*浮点数组;
FILE*FILE=fopen(argv[1],“r”);
fread(&size,sizeof(int),1,文件);
floatArray=(float*)malloc(sizeof(float)*size);
fread(floatArray,sizeof(float),size,file);
对于(int i=0;i
#包括
#为malloc()和exit()函数包含/**/
int main(int argc,char*argv[])
{
文件*文件;
int i,n;/*计数器和浮点数*/
用于存储浮点值的浮点*val;/*指针*/
if(argc)在尝试将文件放入fread函数的*ptr参数时遇到一些严重问题。您读了吗?它告诉您ptr
参数应该是什么?为什么您会认为文件
进入ptr
参数而不是流
参数?即使您没有读手册页,类型也会告诉您。请先仔细阅读手册页,因为它确实回答了您的问题具体问题。当您完成尽职调查后,请回来询问,我仍然不清楚。刚才注意到您正在对打开的文件流和数据缓冲区使用文件
。一个在实际代码中,另一个在问题描述中。在您的实际代码中,情况并非如此,因此请澄清您的确切代码以及exa的内容ct错误适用于该代码。
#include <stdio.h>
#include <stdlib.h> /* for malloc() and exit() function */
int main(int argc, char* argv[])
{
FILE *file;
int i, n; /* the counter and num of floating point numbers */
float* val; /* pointer for storing the floating point values */
if(argc<2)
{
printf( "usage: %s filename", argv[0] );
exit(-1);
}
if((file = fopen( argv[1], "rb" )) ==NULL)
{
printf( "Could not open file.\n" );
exit(-2);
}
if(fread(&n, sizeof(int), 1, file)!=1)
{
printf( "Could not read data from file.\n" );
exit(-3);
};
if((val = malloc(n*sizeof(float)))==NULL)
{
printf( "Could not allocate memory for data.\n" );
exit(-4);
};
printf("Number of data values: %d\n", n);
if(fread(val, sizeof(float), n, file)!=n) /* read each floating point value one by one */
{
printf( "Could not read data from file.\n" );
exit(-5);
}
for(i=0; i<n; ++i)
{
printf("%f \t", val[i]); /* now print it */
}
free(val); /* free the allocated memory */
fclose(file); /* close the file */
return 0;
}