程序代码:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main ()
{
FILE *fp;
char ch1[9]={'\0'};
char ch2[11]={'\0'};
char ch3[11]={'\0'};
char ch4[11]={'\0'};
char ch5[12]={'\0'};
if((fp=fopen("Student.txt","rb"))==NULL)
{
printf("Cannot open file strike any key exit!");
return 0;
}
fseek (fp ,0, SEEK_END);
int lsize = ftell (fp);
fseek (fp ,0, SEEK_SET);
char * rbuf = (char*) malloc (sizeof(char)*lsize+1);
memset(rbuf,'\0',lsize+1);
fread(rbuf,1,lsize,fp);
int i=0;
while(1)
{
strncpy(ch1,rbuf+i,8);
i+=8;
strncpy(ch2,rbuf+i,10);
i+=10;
strncpy(ch3,rbuf+i,10);
i+=10;
strncpy(ch4,rbuf+i,10);
i+=10;
strncpy(ch5,rbuf+i,11);
i+=11;
printf("%8s%10s%10s%10s%11s",ch1,ch2,ch3,ch4,ch5);
memset(ch1,'\0',9);
memset(ch2,'\0',11);
memset(ch3,'\0',11);
memset(ch4,'\0',11);
memset(ch5,'\0',12);
if(i>lsize) break;
}
free(rbuf);
fclose(fp);
return 0;
}
[
本帖最后由 wp231957 于 2014-3-1 18:13 编辑 ]