size_t和大小 [英] size_t and size of

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

问题描述

在fread中,函数的类型是typedef size_t。

我想重写一个读取mp3二进制数据的程序。

int main(){

printf("输入文件名 - >");

char name;

fflush(stdout);

FILE * fp;

fp = fopen(name," rb");

/ *这里是fread应该去的地方但是k& rp 248没有多大帮助* /

fclose(fp);}


----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000个新闻组 - 19个不同的服务器! = -----

In fread, the type of the function is the typedef size_t.
I want to rewrite a program that read binary data of mp3s.
int main(){
printf("Enter name of file-> ");
char name;
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
/*Here''s where fread should go but k&r p 248 didn''t help much */
fclose(fp);}

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

推荐答案

Bill Cunningham< nospam @ net>写道:
Bill Cunningham <nospam@net> wrote:
在fread中,函数的类型是typedef size_t。
我想重写一个读取mp3的二进制数据的程序。
int main(){
printf("输入文件名 - >");
char name;


除非您拥有符合C99标准的编译器,否则您必须在第一个可执行语句之前定义所有变量

。并且

因为你要使用''name'来存储一个文件名,一个

单个字符就不够了。

fflush(stdout);
FILE * fp;
fp = fopen(name," rb");


fopen()期望一个char指针作为它的第一个参数和''name''

既不是char指针也不是它已被初始化...

/ *这里是fread应该去的地方但k& rp 248并没有多大帮助* /
In fread, the type of the function is the typedef size_t.
I want to rewrite a program that read binary data of mp3s.
int main(){
printf("Enter name of file-> ");
char name;
Unless you have a C99 compliant compiler you must define all
your variables before the first executable statement. And
since you''re going to use ''name'' to store a file name, a
single character won''t be enough.
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
fopen() expects a char pointer as its first argument and ''name''
is neither a char pointer nor has it been initalized...
/*Here''s where fread should go but k&r p 248 didn''t help much */




fread()被声明为


size_t fread(void * ptr,size_t size,size_t nobj,FILE * stream)


''tize_t''是只是一个无符号(即它不能小于0)的整数

类型足以容纳你的

机器上所有可能的大小信息。并且fread()读取了许多''nobj''对象,每个对象都是

size''custity''(以char的大小为单位,通常等于一个字节),

来自输入流''stream''(你可以使用你的''fp''

FILE指针)到''指向的缓冲区' PTR。当它返回它时

告诉你它读了多少个对象。


例如,让我们假设你要读100 int''来自文件。

然后你首先需要一个缓冲区,fread()可以在以后存储它们。

所以你要么需要一个阵列


int data [100];


或者你需要一个动态分配的相同大小的缓冲区


int * data;


if((data = malloc(100 * sizeof * data))== NULL)

{

fprintf(stderr, 耗尽内存\ n;)

退出(EXIT_FAILURE);

}


你还需要一个变量来保存

fread()调用将要返回的项目数,即


size_t count;


现在让'我们假设你已经成功打开了文件,

和fopen()的返回值存储在''fp''中。然后你就可以在你的100 int'中读取



count = fread(data,sizeof * data,100,fp);


''data''是数据存储的缓冲区,

''sizeof * data''是单个int的大小(你)也可以用
写''sizeof(int)'',但是你必须改变它,如果

你以后应该决定读取例如long int'来自

文件而不是简单的int'),100是整数的数量
你要读的
和''fp''是一个FILE指针顶部的文件你

想要阅读。


调用fread()后,''count''变量告诉你多少

项(在这种情况下为整数)gort从文件中读取,它可以

小于100时,例如没有那么多的int存储在

文件中如你所料。


请注意:在现实世界中有几种可能陷阱

- 一台机器写的二进制数据可能对于不同的机器而言并不意味着什么...不同的架构。例如,

一台机器可能有4字节int'而另一台机器有2字节

而另一个是小端的。对于浮点数来说,它可能会变得更糟......因此,当你进行二进制读取时,你必须准备好处理所有这些可能出现的问题。


问候,Jens

-

_ _____ _____

| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de

_ | | | | | |

| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring

\ ___ / ens | _ | homs | _ | oerring



fread() is declared as

size_t fread( void *ptr, size_t size, size_t nobj, FILE *stream )

''size_t'' is simply an unsigned (i.e. it can''t be less than 0) integer
type large enough to hold all possible size informations on your
machine. And fread() reads up to a number of ''nobj'' objects, each of
size ''size'' (in units of the size of a char, which often equals a byte),
from the input stream ''stream'' (that''s where you would use your ''fp''
FILE pointer) into a buffer pointed to by ''ptr''. When it returns it
tells you how many objects it has read.

As an example, let''s assume you want to read 100 int''s from a file.
Then you first need a buffer where fread() can later store them.
So you either need an array

int data[ 100 ];

or you need an dynamically allocated buffer of the same size

int *data;

if ( ( data = malloc( 100 * sizeof *data ) ) == NULL )
{
fprintf( stderr, "Running out of memory\n" );
exit( EXIT_FAILURE );
}

You also need a variable to hold the number of items the
fread() call is going to return, i.e.

size_t count;

Now let''s also assume you already opened the file successfully,
and the return value of fopen() is stored in ''fp''. Then you can
read in your 100 int''s as

count = fread( data, sizeof *data, 100, fp );

''data'' is the buffer the data are going to be stored in,
''sizeof *data'' is the size of a single int (you could also
write ''sizeof( int )'', but then you have to change this if
you should later decide to read e.g. long int''s from the
file instead of simple int''s), 100 is the number of integers
you want to read and ''fp'' is a FILE pointer top the file you
want to read from.

After the call of fread() the ''count'' variable tells you how many
items (integers in this case) gort read from the file, it could
be less than 100 when e.g. there weren''t as many int''s stored in
the file as you expected.

Please note: in the real world there are several possible pitfalls
- binary data written by one machine might not mean a thing to a
different machine with e.g. a different architecture. For example,
one machine might have 4 byte int''s while another one has 2 byte
int''s, or one machine might store numbers in big-endian format,
while the other in small-endian. And for floating point numbers
it might get even worse... Thus when you do binary reads you must
be prepared to deal with all these possible problems.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring


2003年11月1日星期六13:34:45 -0500,Bill Cunningham < nospam @ net>

写道:
On Sat, 1 Nov 2003 13:34:45 -0500, "Bill Cunningham" <nospam@net>
wrote:
在fread中,函数的类型是typedef size_t。
我想重写一个读取mp3的二进制数据的程序。
int main(){
printf("输入文件名>");
char名称;
fflush(stdout );
FILE * fp;
fp = fopen(名字,rb);
/ *这里是fread应该去的地方但k& rp 248没有帮助很多* /
fclose(fp);}
In fread, the type of the function is the typedef size_t.
I want to rewrite a program that read binary data of mp3s.
int main(){
printf("Enter name of file-> ");
char name;
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
/*Here''s where fread should go but k&r p 248 didn''t help much */
fclose(fp);}



fread返回size_t作为读取的字节数。这没有什么与实际数据传输到缓冲区有关的其他内容。

fread会读取二进制文件就好了。

< ;<删除电子邮件的del>>


fread returns a size_t as the count of bytes read. This has nothing
else to do with the actual data being transferred to the buffer.
fread will read a binary file just fine.
<<Remove the del for email>>


你说fread期望一个指向char的指针作为它的第一个参数。那个

是否意味着它除了chars之外呢?

或者我想我应该说字符串可能吗?我已经看过很多了。


fp = fread(" hello.exe"," rb");

第一个参数被接受为字符串而不是字符,或指向

字符的指针。对吧?


比尔


----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000个新闻组 - 19个不同的服务器! = -----
You say fread expects a pointer to a char as it''s first argument. Does that
mean it will except chars too?
or I guess I should say strings maybe? I''ve seen a lot of this.

fp=fread("hello.exe","rb");
The first argument is accepted as a string not a char, or a pointer to a
char. Right?

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


这篇关于size_t和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆