C fwrite函数疑问

giftboys_8 2008-11-26 10:06:15
在我使用的fwrite中书中是这样定义的:
int fwrite(void *buf,int size,int count,FILE *strearn)
但我使用中发现是这样的。
int fwrite(void *buf,int count,int size,FILE *strearn)

有没有兄弟知道为什么会这样?
附代码
#include<stdio.h>
FILE *fp;
main()
{
fp=fopen("c:\\1.txt","w");
put_rec(fp,"2222222222222222");
}
put_rec(fp,rec)
FILE *fp;
int rec[6];
{
int size,len;
size=sizeof(rec);
len=fwrite(rec,8,size,fp);
fclose(fp);
if(len!=size) printf("Write Error");
}
...全文
2304 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
mifeixq 2008-12-11
  • 打赏
  • 举报
回复
其实这个问题很好理解,关键就是要理解
int fwrite(void *buf,int count,int size,FILE *strearn)
这里的 int count 和 int size 的含义~~

初用的时候可能搞不懂这两个有什么区别~

这里的count 看字面就是计数的东西,而size有大小的含义

那么看看具体是怎么回事吧
man 了一下fwrite
函数原型:
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
说明:
The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream,
obtaining them from the location given by ptr.

可以看得出来,fwrite是往stream里面写入count个size大小的元素,那么好

如果是写入一个字符串,假设是"abcd",那么count=4, size=1是一种解释情况,写入4个1字节字符
count = 1, size=4的话,那就是写入1个4字节大小的字符串……

其实多用用就好理解了,同时注意查看函数原型,windows下没有man就多上网搜搜吧
lbh2001 2008-12-11
  • 打赏
  • 举报
回复
库函数的基本用法
查看一下库函数的权威说明就清楚了
不要尽信书,实践才是真知
tianjiao85 2008-12-11
  • 打赏
  • 举报
回复
趣怪的语法,
fuzhufang 2008-12-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 mifeixq 的回复:]
其实这个问题很好理解,关键就是要理解
int fwrite(void *buf,int count,int size,FILE *strearn)
这里的 int count 和 int size 的含义~~

初用的时候可能搞不懂这两个有什么区别~

这里的count 看字面就是计数的东西,而size有大小的含义

那么看看具体是怎么回事吧
man 了一下fwrite
函数原型:
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
说…
[/Quote]
顶。
还望楼主要多看有权威的文档,自己都动手实践
jiww03 2008-12-11
  • 打赏
  • 举报
回复
100个1和1个100是一样的
xiaopoy 2008-12-11
  • 打赏
  • 举报
回复
[Quote=引用 的回复:]
size=sizeof(rec);//rec是一个指针,sizeof算出来结果是4,而不是上面2的个数

二进制要小心打开模式

库函数的基本用法
查看一下库函数的权威说明就清楚了
不要尽信书,实践才是真知
[/Quote]
纯帮顶接分.
huangpeng8612 2008-12-11
  • 打赏
  • 举报
回复
在WINDOWS下的时候,打开模式很重要.LINUX下的话是否以二进制打开就不用指定了.
  • 打赏
  • 举报
回复
[Quote=引用楼主 giftboys_8 的帖子:]
在我使用的fwrite中书中是这样定义的:
int fwrite(void *buf,int size,int count,FILE *strearn)
但我使用中发现是这样的。
int fwrite(void *buf,int count,int size,FILE *strearn)
[/Quote]
不管是什么顺序,性质是一样的

toadzw 2008-12-11
  • 打赏
  • 举报
回复
#include <stdio.h>

struct mystruct
{
int i;
char ch;
};

int main(void)
{
FILE *stream;
struct mystruct s;

if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
s.i = 0;
s.ch = 'A';
fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
fclose(stream); /* close file */
return 0;
}
二进制要小心打开模式
二进制要小心打开模式
二进制要小心打开模式
xxgamexx 2008-11-29
  • 打赏
  • 举报
回复
呵呵 楼主加油!
CedarDiao 2008-11-29
  • 打赏
  • 举报
回复
哈哈,确实是N年前的风格了,在老版本的Linux下存在大量这样的代码。。。
xhs_lh04 2008-11-29
  • 打赏
  • 举报
回复
#include <stdio.h>
FILE *fp;
main()
{
fp=fopen("c:\\1.txt","w");
put_rec(fp,"2222222222222222");
}
put_rec(fp,rec)
FILE *fp;
int rec[6];
{
int size,len;
size=strlen(rec);
len=fwrite(rec,size,1,fp);
fclose(fp);
if(len!=size) printf("Write Error");
}

再试;
ARLENE_YANG 2008-11-29
  • 打赏
  • 举报
回复
我见到的都是以下形式
int fwrite(void *buf,int size,int count,FILE *strearn)

对于LZ提到的问题,我也不清楚
顶一下
楼下继续
giftboys_8 2008-11-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ztz0223 的回复:]
引用楼主 giftboys_8 的帖子:
在我使用的fwrite中书中是这样定义的:
int fwrite(void *buf,int size,int count,FILE *strearn)
但我使用中发现是这样的。
int fwrite(void *buf,int count,int size,FILE *strearn)

有没有兄弟知道为什么会这样?
附代码
#include <stdio.h>
FILE *fp;
main()
{
fp=fopen("c:\\1.txt","w");
put_rec(fp,"2222222222222222");
}
put_rec(fp,rec)
FILE *fp;
int …
[/Quote]
兄弟说的对!
leiminlovesoft 2008-11-27
  • 打赏
  • 举报
回复
再仔细检查一下你的代码哟。
waizqfor 2008-11-27
  • 打赏
  • 举报
回复
#include <stdio.h>

struct mystruct
{
int i;
char ch;
};

int main(void)
{
FILE *stream;
struct mystruct s;

if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
s.i = 0;
s.ch = 'A';
fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
fclose(stream); /* close file */
return 0;
}

这应该是正确的
Claude 2008-11-27
  • 打赏
  • 举报
回复
书上定义是正确的哈……说实话,没搞懂那段代码,竟然还能运行……望大牛解释下……
就呆在云上 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 giftboys_8 的帖子:]
在我使用的fwrite中书中是这样定义的:
int fwrite(void *buf,int size,int count,FILE *strearn)
但我使用中发现是这样的。
int fwrite(void *buf,int count,int size,FILE *strearn)

有没有兄弟知道为什么会这样?
附代码
#include <stdio.h>
FILE *fp;
main()
{
fp=fopen("c:\\1.txt","w");
put_rec(fp,"2222222222222222");
}
put_rec(fp,rec)
FILE *fp;
int rec[6];
{
int size,len;

[/Quote]

fwrite的使用是按照二进制的位进行的,因此你的代码问题不是fwrite的size和count的问题
问题出在文件打开上面,你把程序修改一下:
#include <stdio.h> 
FILE *fp;
main()
{
fp=fopen("c:\\1.txt","wb+"); ///////二进制打开呵呵
put_rec(fp,"2222222222222222");
}
put_rec(fp,rec)
FILE *fp;
int rec[6];
{
int size,len;
size=sizeof(rec);
len=fwrite(rec,8,size,fp);
fclose(fp);
if(len!=size) printf("Write Error");
}
lujun723 2008-11-26
  • 打赏
  • 举报
回复
书没错,上面代码fwrite不对
ZhengZhiRen 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fengxuxing 的回复:]
这段代码让我见到了二十年前的代码风格,实属难得!!!大家可以来围观一下、
[/Quote]
为什么?
加载更多回复(1)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧