阅读背景:

如何使用fwrite()函数在文件中一次编写结构成员?

来源:互联网 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct student{
    char *name;
    char *addr;
    int age;
   int clas;
 }*stu;

 int main()
 {
    FILE *fp;
    int choice,another;
    size_t recsize;
    size_t length;

   struct student *stu=(struct student *)malloc(sizeof(struct student));
   stu->name=(char *) malloc(sizeof(char)*20);
   stu->addr=(char*)malloc(sizeof(char)*20);

   recsize=sizeof(*stu);


           fp=fopen("student.txt","a+");
            if(fp==NULL)
               {
                 fp=fopen("student.txt","w+");
                 if(fp==NULL)
                  {
                    printf("cannot open the file");
                    exit(1);
                  }  
                }

                do
                 {
                   fseek(fp,1,SEEK_END);
                   printf("Please Enter student Details\n");

                   printf("Student Name: ");
                   scanf("%s",stu->name);

                    printf("Address: ");
                    scanf("%s",stu->addr);

                    printf("Class: ");
                    scanf("%s",&stu->clas);

                    printf("Age: ");
                    scanf("%s",&stu->age);                     

                    fwrite(stu,recsize,1,fp);

                    printf("Add another Enter 1 ?\n");
                    scanf("%d",&another);
                   }while(another==1);
                  fclose(fp);
        free(stu);


   }
#include<stdio.h>
#include<conio.h>
#include<st



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: