| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 538 人关注过本帖
标题:fread的问题
只看楼主 加入收藏
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:4 
fread的问题
Write a program which asks user to enter a file name.
Program then reads array of points from the file.
The file contains data that was generated by ex. 30.
In the end program prints all points and total number of
points that was read from the file.
Program can use a fixed maximum value (= size of the array to hold points)
which limits how many points can be read from the file.



#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

#define MY_FILENAME "points.txt"
#define LINESIZE 80
#define FILESIZE 15
#define SIZE 2
#define PTSIZE 10

struct points
{
  int x;
  int y;
}pt[PTSIZE],*cdt;
int main()
{
  FILE *fp;
  cdt=pt;
  int i;
  char filename[FILESIZE]="points.txt",fileinput[FILESIZE],*fileptr;/*share the same file with no.30,so use the address form..*/
  printf("Please enter a filename!\n");
  scanf("%s",fileinput);
  fileptr=strstr(filename,fileinput);
  if (fileptr)
  {
  if ((fp=fopen("c:\\points.txt","rb+"))==NULL)
  {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
  }
  else
  {
  for (i=0;i<SIZE;i++)
  {
  fread(&pt[i],sizeof(struct points),SIZE,fp);/*此处的SIZE值是死的,全局变量里面设定好了,请问如何实现自动扫描,有几个点显示几个点呢。我试了试fscanf,但好像因为是二进制所以扫除来的都是0,不止到怎么办,请高手解答,谢谢*/

  }
  printf("\n");
  printf("the points are:\n");
  }

  }
  else
  {
  printf("the file doesn't exist!\n");
  }
  for (i=0;i<SIZE;i++)
  {
  printf("%d %d\n",pt[i].x,pt[i].y);
  }
  return 0;
}
搜索更多相关主题的帖子: fread 
2010-05-13 23:54
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
你用什么进去的?
如果是fprintf就要用fscanf去读出来。才能正确。
如果是fwrite才用fread去读

南国利剑
2010-05-14 00:00
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
收藏
得分:0 
回复 2楼 南国利剑
我是用fwrite写的文件

#include <stdio.h>
#include <stdlib.h>
#include  <conio.h>

#define MY_FILENAME "points.txt"

struct points
{
    int x;
    int y;
}pt[10],*cdt;
int main()
{
    FILE *fp;
    cdt=pt;
    int ptnum=0;
    int i;
    printf("how many points do you want to enter?\n");
    scanf("%d",&ptnum);
    if (!ptnum)
    {
        printf("wrong input\n");
    }
    else
    {
        printf("please enter coordinates x and y\n");
        if ((fp=fopen("c:\\points.txt","wb+"))==NULL)/*since no.31 share the same file with this problem*/
        {
            printf("Cannot open file strike any key exit!");
            getch();
            exit(1);
        }
        for (i=0;i<ptnum ;i++,cdt++ )
        {
            printf("please enter point %d\n",i+1);
            scanf("%d\n%d",&cdt->x,&cdt->y);
        }
        cdt=pt;
        fwrite(cdt,sizeof(struct points),ptnum,fp);
        rewind(fp);
        fclose(fp);
    }
    return 0;
}

But here I am
2010-05-14 00:03
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:10 
我不知道你说的具体是什么意思!你的意思是说在你打开你所建立的文件里看见的全是0吗?fread(&pt[i],sizeof(struct points),SIZE,fp);这里设置的不是SIZE,而应该是1。

愿用余生致力编程
2010-05-14 08:53
xu362726904
Rank: 6Rank: 6
等 级:侠之大者
帖 子:160
专家分:471
注 册:2009-6-18
收藏
得分:10 
你用for循环的话,size的值应该为1,fread(pt,sizeof(struct pointf),SIZE,fp);这样不要循环
2010-05-14 14:00
快速回复:fread的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012335 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved