| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 538 人关注过本帖
标题:fread的问题
取消只看楼主 加入收藏
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:1 
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
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
快速回复:fread的问题
数据加载中...
 
   



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

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