简体   繁体   English

C中的fread和fwrite参数

[英]fread and fwrite parameters in C

I am trying to adapt a matlab program to C, my problem is in the fwrite and fread function. 我正在尝试将matlab程序改编为C,我的问题在于fwritefread函数。 On matlab I have: 在matlab上我有:

fid = fopen ('sweep_100_3400.pcm','rb');
s = fread (fid, 'int16');

My doubt is, in C there are two more parameters in fread and fwrite function. 我的疑问是,在C中, freadfwrite函数还有两个参数。

fread(void*, size_t, size_t, FILE*);
fwrite(const void*, size_t, size_t, FILE*);

In my C code I have: 在我的C代码中,我有:

arq = fopen("C:\\Users\\iago_\\Desktop\\MediaMovel\\sweep_100_3400.pcm", "rb");
fread(x, sizeof(double), itera, arq);
fclose(arq);

x is the vector where the data of my file will be saved. x是我的文件数据将被保存的向量。
sizeof(double) is the length of the data (I've declared all double ) sizeof(double)是数据的长度(我声明了所有的double
arq is the pointer to the file. arq是指向文件的指针。

The third parameter is a size_t , to adapt my matlab program, in this parameter should I use the media length or the vector size? 第三个参数是size_t ,为了适应我的matlab程序,在这个参数中我应该使用媒体长度还是向量大小?

(I am encoding a moving average, the media length is informed by the user and the vector size is the length of my input file). (我正在编码移动平均线,媒体长度由用户通知,矢量大小是我输入文件的长度)。

For the fwrite function I have the same doubt about the parameters. 对于fwrite函数,我对参数有同样的疑问。

arq=fopen("C:\\Users\\iago_\\Desktop\\MediaMovel\\saida_medial_movel_c.pcm", "wb");
fwrite(saida, sizeof(double), itera, arq);
fclose(arq); 

fread() parameters are fread()参数是

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

where nmemb is multiple of size function of read. 其中nmemb是读取的size函数的倍数。 So second parameter should be sizeof your vector and third should number of vectors you want to read from file pointer. 因此,第二个参数应该是矢量的大小,第三个参数应该是您想要从文件指针读取的矢量数。 Also there is but of confusion about matlab fread call. 还有关于matlab fread调用的混乱。 second param in matlab fread call is int16 which is 2 bytes, but in c fread call you have passed second param as sizeof(double) which is 8 bytes. matlab fread调用中的第二个参数是int16,它是2个字节,但在c fread调用中,你已经传递了第二个参数sizeof(double),即8个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM