Search code examples
cfile-iofseek

difficulty with reading the last record from a .txt file in C


I am writing a program in C that reads file data (name, answer of q1, q2, q3) from the user, and store the data in a .txt file, and allow the user to view the data they have entered. Currently, I have difficulty with the function that allows data to see the last record. Here is my code:

struct pre_survey{
char name[20];
int q1;
int q2;
int q3;
};
struct pre_survey temp;
struct pre_survey get;

int main(){
while (pre_or_post!=0){

if(pre_or_post==1){
    printf("\n1. append(enter) data\n");
    printf("2. check first record\n");
    printf("3. check last record\n");
    printf("please select your choice\n");
    scanf("%d", &choice);

switch (choice){
    case 1: append();
            break;
    case 2: frst_record();
            break;
    case 3: last_record();
            break;}}

void append(){
    fp=fopen("pre-survey.txt", "a");
    printf("name\n");
    scanf("%s", &temp.name);
    printf("q1:\n");
    scanf("%d", &temp.q1);
    printf("q2:\n");
    scanf("%d", &temp.q2);
    printf("q3:\n");
    scanf("%d", &temp. q3);
    fprintf(fp, "%s %d %d %d", temp.name, temp.q1, temp.q2, temp.q3);
    fclose(fp);}

void last_record(){
    fp=fopen("pre-survey.txt", "r");
    fseek(fp, -sizeof(temp),SEEK_END);
    fscanf(fp, "%s %d %d %d", get.name, &get.q1, &get.q2, &get.q3);
    printf("name: %s\n", get.name);
    printf("q1: %d\n", get.q1);
    printf("q2: %d\n", get.q2);
    printf("q3:%d\n", get.q3);
    fclose(fp); 
}

Right now, when I try to find the last record, the data for the first record shows up. I think the problem is that when I check the sizeof(temp) is 32, and when I use

fp=fopen("pre-survey.txt", "r");
fseek(fp,0, 2);
size=ftell(fp);
printf("size:%d\n", size);

to check the size of the whole file, it is 34. So when I am reading from the end of the file by size of temp, it goes to the first record. But I am not sure what I did wrong in the code.


Solution

  • If the position to read the record by fseek, Record must be a fixed length.

    E.g.

    void append(void){
        FILE *fp=fopen("pre-survey.txt", "a");
        printf("name\n");
        scanf("%19s", temp.name);
        printf("q1:\n");
        scanf("%d", &temp.q1);
        printf("q2:\n");
        scanf("%d", &temp.q2);
        printf("q3:\n");
        scanf("%d", &temp. q3);
        //32bit system MAX_INT : 2147483647 (10digits)
        //19char  2digit 2digit 2digit\n total:30 (windows:31(\n->CRLF)
        fprintf(fp, "%-20s%3d%3d%3d\n", temp.name, temp.q1, temp.q2, temp.q3);
        fclose(fp);
    }
    
    void last_record(void){
        FILE *fp=fopen("pre-survey.txt", "rb");
        //fseek(fp, -31, SEEK_END);//for windows
        fseek(fp, -30, SEEK_END);
        fscanf(fp, "%s %d %d %d", get.name, &get.q1, &get.q2, &get.q3);
        printf("name: %s\n", get.name);
        printf("q1: %d\n", get.q1);
        printf("q2: %d\n", get.q2);
        printf("q3: %d\n", get.q3);
        fclose(fp); 
    }