免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1774 | 回复: 4
打印 上一主题 下一主题

高人帮忙看一下 fread 与 fwrite 的问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-11 17:04 |只看该作者 |倒序浏览
下面是小弟的一段C代码:(已修改~)
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        int main()
        {
                FILE *fp;
                char *ch_in;
                char *ch_out;
                int size;
               
                if ((fp = fopen("hello.dat","wb")) == NULL)
                        {
                                printf(" Open error! Check it !\n");
                                return -1;
                        }
                ch_in = " Hello World !";
                printf("The input strings is : %s\n",ch_in);
        /*
                size = sizeof(*ch_in);  请看gusing的回复~
       */
                size = strlen(ch_in)+1;

                printf("\n The size is : %d\n",size);
               
                fwrite(ch_in,size,1,fp);
               
                fclose(fp);
                fp = fopen("hello.dat","rb");
               
                printf("\nThe output strings would be : ");
               
                fread(ch_out,size, 1, fp);
               
                printf("%s\n",ch_out);

                return 0;
        }

输出结果:
The input strings is :  Hello World !

The size is : 1

The output strings would be :  

论坛徽章:
0
2 [报告]
发表于 2009-09-11 17:06 |只看该作者
size = sizeof(*ch_in);

相当于

size = sizeof(char)

改成
size = strlen(ch_in)+1;

论坛徽章:
0
3 [报告]
发表于 2009-09-11 19:34 |只看该作者

回复 #2 gussing 的帖子

谢谢您的回复,果然如此!

论坛徽章:
0
4 [报告]
发表于 2009-09-11 20:10 |只看该作者

回复 #1 maeam 的帖子

又试了一下整型数组的存放,希望对新手有用~
Warning: 文件在写入的时候设定的格式是”wb",在读入的时候应该先关闭,然后设定为"rb",代码如下:
        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
       
        int main()
        {
                FILE *fp;
                int a[5];
                int b[5];
                int i;
                int size;
               
                for (i=0;i<5;i++)
                        {
                                a = i;
                                printf("a[%d] is : %d\n",i,a);
                        }
               
               
                if ((fp=fopen("int_array.dat","wb")) == NULL)
                        {
                                printf("Open error! Check it!\n");
                                getchar();
                                return -1;
                        }
                       
                size = sizeof(a);
                printf("The array's length is : %d\n",size);
               
                fwrite(&a,size,1,fp);
               
                //fseek(fp,0,0);
               
                fclose(fp);
                if ((fp=fopen("int_array.dat","rb")) == NULL)
                        {
                                printf("Open error! Check it!\n");
                                getchar();
                                return -1;
                        }
                fread(&b,size,1,fp);
       
               
                for(i=0;i<5;i++)
                {
                       
                        printf("%d\n",b);
                }
       
                //printf("%d\n",b[0]);       

                return 0;
               
        }

[ 本帖最后由 maeam 于 2009-9-11 20:11 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-09-11 20:12 |只看该作者

回复 #4 maeam 的帖子

字体有些改变,“for”中的“[]”不能显示,谁用谁再加阿~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP