实验5


#include<stdio.h>
#define N 5
#define M 80

typedef struct
{
    char name[M];
    char author[M];
}Book;


int main()
{
    Book x[N]={{"一九八四","乔治.奥威尔"},
               {"美丽新世界","赫胥黎"},
               {"昨日的世界", "斯蒂芬.茨威格"},
               {"万历十五年","黄仁宇"},
               {"一只特立独行的猪","王小波"}
               };
    int i;
    FILE*fp;
    fp=fopen("data1.txt","w");
    if(fp==NULL)
    {
        printf("fail to open file/n");
        return 1;
    }
    for(i=0;i<N;++i)
    {
        fprintf(fp,"%-20s %-20s/n",x[i].name,x[i].author);
        printf("%-20s %-20s/n",x[i].name,x[i].author);
    }
    
    fclose(fp);
    
    return 0;
}
                        

实验5

#include<stdio.h>
#define N 5 
#define M 80

typedef struct
{
    char name[M];
    char author[M];
}Book;


int main()
{
    Book x[N];
    int i;
    
    FILE*fp;
    fp=fopen("data1.txt","r");
    if(fp==NULL)
    {
        printf("fail to open file/n");
        return 1;
    }
    for(i=0;i<N;++i)
    {
        fscanf(fp,"%s %s/n",x[i].name,x[i].author);
        printf("%-20s %-20s/n",x[i].name,x[i].author);
    }
    fclose(fp);
    return 0;
}

实验5

地址已经算进去了

#include<stdio.h>
#define N 5
#define M 80

typedef struct
{
    char name[M];
    char author[M];
}Book;


int main()
{
    Book x[N]={{"一九八四","乔治.奥威尔"},
               {"美丽新世界","赫胥黎"},
               {"昨日的世界", "斯蒂芬.茨威格"},
               {"万历十五年","黄仁宇"},
               {"一只特立独行的猪","王小波"}
               };
    int i;
    FILE*fp;
    fp=fopen("data2.txt","wb");
    if(fp==NULL)
    {
        printf("fail to open file/n");
        return 1;
    }
    fwrite(x,sizeof(Book),N,fp);

    fclose(fp);
    
    return 0;
}
                        

实验5

#include<stdio.h>
#define N 5 
#define M 80

typedef struct
{
    char name[M];
    char author[M];
}Book;


int main()
{
    Book x[N];
    int i;
    
    FILE*fp;
    fp=fopen("data2.txt","rb");
    if(fp==NULL)
    {
        printf("fail to open file/n");
        return 1;
    }
    fread(x,sizeof(Book),N,fp);
    for(i=0;i<N;++i)
    
        printf("%-20s %-20s/n",x[i].name,x[i].author);
    
    fclose(fp);
    return 0;
}

实验5

 

原创文章,作者:sunnyman218,如若转载,请注明出处:https://blog.ytso.com/267062.html

(0)
上一篇 2022年6月14日
下一篇 2022年6月14日

相关推荐

发表回复

登录后才能评论