C语言文件读写练习

文件读写内容前面已经讲过了,这里有道题,随便贴出来。

#include 

struct student
{
  long studentNumber;
  char *firstName;
  char *lastName;
};

/* the function returns the address of an array of student-typed objects */
struct student *readStudentRecordFile(char *fileName, int *numOfStudents)
{

    /* fill in here */

}


/* the function writes an array of student-typed objects into a file */
void writeStudentRecordFile(char *fileName, struct student *AllStudents, int *numOfStudents)
{

    /* fill in here */

}


/* the function sorts an array of student-typed objects according to studentNumber
and returns a sorted array of such objects */
struct student *sortStudents(struct student *AllStudents, int numOfStudents)
{
    /* fill in here */
}

/* the function capitaizes the letters in the student namess for an array of
student-type objects
*/
void capitalizeNames(struct student *AllStudents, int numOfStudents)
{

    /* fill in here */

}

int main(void)
{

    int nStudents;
    struct student *readArray;
    struct student *sortedArray;

    readArray=readStudentRecordFile("homework8.dat", &nStudents);
    capitalizeNames(readArray, nStudents);
    sortedArray=sortStudents(readArray, nStudents);
    writeStudentRecordFile("homework8_out.dat", sortedArray, &nStudents);

    return 0;

} 

不完整的答案吧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
   long studentNumber;
   char * firstName;
   char * lastName;
};

/* the function returns the address of an array of student-typed objects */
struct student *readStudentRecordFile(char *fileName, int *numOfStudents)
{

    /* fill in here */
 int i=0;
 struct student *p=(struct student*)malloc(sizeof(struct student));
 static struct student AllStudents[10];
 char * firstName;
 char * lastName;
 long num;


    FILE *fp;
    fp=fopen(fileName,"r");
 if (fp == NULL)
 {
  printf("open file error!\n");
  exit(1);
 }

    fscanf(fp,"%d\n",numOfStudents);   //read total num;   loss a & 
// printf("%d\n",numOfStudents);
 

//     for(i=0;i<10;i++){
//         fscanf(fp,"%s\t%s\t%l\n",&AllStudents[i].firstName,&AllStudents[i].lastName,&AllStudents[i].studentNumber);
//  }

 for ( p= AllStudents;p<AllStudents+10;p++)
 {
  firstName = (char *)(malloc(10));
  lastName = (char *)(malloc(10));
  fscanf(fp,"%s\t%s\t%ld\n",firstName,lastName,&num);
  p->firstName = (char *)(malloc(10));
  strcpy(p->firstName,firstName);
  p->lastName = (char *)(malloc(10));
  p->lastName = lastName;
  p->studentNumber = num;
  //printf("%s %s %ld\n",firstName,lastName,num);
  //printf("%s %s %ld\n",p->firstName,p->lastName,p->studentNumber);
 }
 
//  for(p=AllStudents;p<AllStudents+10;p++){
//         printf("%s %s %ld\n",p->firstName,p->lastName,p->studentNumber);
//  }
 fclose(fp);

 p=AllStudents;
 return p;
}


/* the function writes an array of student-typed objects into a file */
void writeStudentRecordFile(char *fileName, struct student *AllStudents, int *numOfStudents)
{

    /* fill in here */

    int i;

    FILE *fp;
    fp=fopen(fileName,"w");

    fprintf(fp,"%d\n",*numOfStudents);
    for(i=0;i<10;i++){
        fprintf(fp,"%s\t%s\t%ld\n",((AllStudents+i)->firstName),((AllStudents+i)->lastName),((AllStudents+i)->studentNumber));
 }
    fclose(fp);


}


/* the function sorts an array of student-typed objects according to studentNumber
and returns a sorted array of such objects */
struct student *sortStudents(struct student *AllStudents, int numOfStudents)
{
    /* fill in here */
    struct student tmp;
    int i;
    int j;
//    int n;

    for (i=0;i<10;i++)
    {
        //n=0;
        for(j=0;j<10;j++)
        {
            if(AllStudents[i].studentNumber<AllStudents[j].studentNumber){

    tmp=AllStudents[j];
    AllStudents[j]=AllStudents[i];
    AllStudents[i]=tmp;
   }
                //n++;
        }
    }

    return AllStudents;
}

/* the function capitaizes the letters in the student namess for an array of
student-type objects
*/
void capitalizeNames(struct student *AllStudents, int numOfStudents)
{
    /* fill in here */
    int i;
    int j;
    for(i=0;i<numOfStudents;i++)
    {
        j=0;
        do
        {
            AllStudents[i].firstName[j]=toupper(AllStudents[i].firstName[j]);
            j++;
        }while(AllStudents[i].firstName[j]!='\0');
  //printf("%s\n",AllStudents[i].firstName);
        j=0;
        do
        {
            AllStudents[i].lastName[j]=toupper(AllStudents[i].lastName[j]);
            j++;
        }while(AllStudents[i].lastName[j]!='\0');
  //printf("%s\n",AllStudents[i].lastName);
    }
}

int main(void)
{

    int nStudents;
    struct student *readArray;
 int i;
    struct student *sortedArray;
 struct student *tmp;
    readArray=readStudentRecordFile("homework8.txt", &nStudents);
 tmp = readArray;
 printf("%d\n",nStudents);
 for (i =0;i<10;i++,tmp++)
 {
  printf("%s\t%s\t%ld\n",tmp->firstName,tmp->lastName,tmp->studentNumber);
 }
 printf("\n\n");
  capitalizeNames(readArray, nStudents);
 tmp = readArray;
 for (i =0;i<10;i++,tmp++)
 {
  printf("%s\t%s\t%ld\n",tmp->firstName,tmp->lastName,tmp->studentNumber);
 }
 printf("\n\n");
 sortedArray=sortStudents(readArray, nStudents);
 tmp = sortedArray;
 for (i =0;i<10;i++,tmp++)
 {
  printf("%s\t%s\t%ld\n",tmp->firstName,tmp->lastName,tmp->studentNumber);
 }
    writeStudentRecordFile("homework8_out.txt", sortedArray, &nStudents);
    return 0;
}

源代码Extra.rar

Related Articles

0 评论 :

发表评论

Quote Of The Day