J
J
jillene2015-06-01 14:24:31
Books
jillene, 2015-06-01 14:24:31

How to read data from a file with a condition?

Can you please tell me how to read data from a file into a linked list with a condition. That is, read only those records that have the field place = street.
Structure:

typedef struct S_RacingCar {

  char name[12];
  char place[12];
  int speed;
  struct S_RacingCar *next;
  struct S_RacingCar *previous;
  
} RacingCar;

Reading from a file
RacingCar *ReadNextFromFile(RacingCar *start, FILE *pFile) {
    size_t returnValue;
    if(start == NULL) {
        start = malloc(sizeof(RacingCar));
        returnValue = fread(start, sizeof(RacingCar), 1, pFile);
        start->next = NULL;
        start->previous = NULL;
    } else {
        RacingCar *indexCar = start;
        RacingCar *newCar = malloc(sizeof(RacingCar));
        while(indexCar->next != NULL) {
            indexCar = indexCar->next;
        }
        returnValue = fread(newCar, sizeof(RacingCar), 1, pFile);
        indexCar->next = newCar;
        newCar->next = NULL;
        newCar->previous = indexCar;
    }
    return start;
}
 
RacingCar *ReadListIn(RacingCar *start) {
     
    FILE *pFile;
    pFile = fopen("myList.bin", "rb");
    if(pFile != NULL) {
     
        CleanUp(start);
        start = NULL;
         
        fseek(pFile, 0, SEEK_END);
        long fileSize = ftell(pFile);
        rewind(pFile);
         
        int numEntries = (int)(fileSize / (sizeof(RacingCar)));
        printf("numEntries:%d\n",numEntries);
         
        int loop = 0;
        for(loop = 0; loop < numEntries; ++loop) {
            fseek(pFile, (sizeof(RacingCar) * loop), SEEK_SET);
            start = ReadNextFromFile(start, pFile);
        }
    }  else {
        printf("FILE OPEN ERROR FOR READ\n");
    }
     
    return start;
 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Spetros, 2015-06-01
@Spetros

You check the compliance of the field of the structure with the given condition when reading from the file - that's all.
Only now the program is very similar to someone else's version of the lab, which you want to pass off as your work according to your own version. First, understand the fields and their naming.
For an ignoramus, no one here will do labs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question