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;
}