This is a question related to the C language structure.

Asked 2 years ago, Updated 2 years ago, 92 views

I'm posting a question because I don't understand the results of the execution during C language coding. The contents of the txt file in the directory are as follows:

4
10:00 12:00 Lectures
12:00 13:00 Lunch, like always
13:00 15:00 Boring lectures
16:45 17:45 Reading

The code is also as follows:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct SCHEDULE
{
    char* time1; //hh:mm
    char* time2; //hh:mm
    int time1_h; //hh
    int time1_m; //mm
    int time2_h; //hh
    int time2_m; //mm
    char* appointment;
}Schedule;

void Split_line(FILE* ifp, int schedule_num, Schedule *schedule);
void Split_time(int schedule_num, Schedule *schedule);

int main(void)
{
    FILE *ifp;
    int schedule_num=0;
    int i;
    Schedule *schedule;

    ifp = fopen("input.txt", "r");
    fscanf(ifp,"%d\n", &schedule_num);

    schedule = (Schedule*)malloc(sizeof(Schedule)*schedule_num);

    Split_line (ifp, schedule_num, schedule); // Split each line into time1, time2, and attachment
    //Split_time(schedule_num, schedule); // Split time1, time2 of each line
}

void Split_line(FILE* ifp, int schedule_num, Schedule *schedule){

    int i;
    char buff[255];
    char* tmp;

    printf("%d\n",schedule_num);

    for(i=0; i<schedule_num; i++){
        //printf("%d\n", i);
        fgets(buff, 255, (FILE*)ifp); // Read each line
        tmp = strtok(buff), "; //time1 Detach
        schedule[i].time1 = tmp;
        tmp = strtok(NULL, "); //time2 Detach
        schedule[i].time2 = tmp;
        tmp = strtok(NULL, "\n"); //appointments to disconnect
        schedule[i].appointment = tmp;

        printf("%s %s %s\n",schedule[i].time1, schedule[i].time2, schedule[i].appointment);
    }
    printf("\nCheck\n");
    printf("%s %s %s\n",schedule[0].time1, schedule[0].time2, schedule[0].appointment);
    printf("%s %s %s\n",schedule[1].time1, schedule[1].time2, schedule[1].appointment);
    printf("%s %s %s\n",schedule[2].time1, schedule[2].time2, schedule[2].appointment);
    printf("%s %s %s\n",schedule[3].time1, schedule[3].time2, schedule[3].appointment);
        fclose(ifp);        
}   
}

The result of the execution. Can you tell me why schedule[0], schedule1, schedule[2], schedule[3] are properly inserted when the for door is turned and then checked again right after the for door is over, and all of them are changed to [3] values?

struct c

2022-09-22 20:44

1 Answers

I don't know exactly why, but when dealing with arrays and strings, invading the memory area is the most careful and difficult.The results that we solved while debugging are as follows.

After creating a schedule array with malloc, secure a space with malloc about 50byte in the char array of each structure, and fill the trash spaces with 0 with memset. Next, do not directly substitute tmp into the char array of the structure, but attach strcat to tmp substitution and it will be printed well without any problems.

I'm sure other masters will leave a problem with the code in the text or a better code K

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable: 4996)

typedef struct SCHEDULE
{
    char* time1; //hh:mm
    char* time2; //hh:mm
    int time1_h; //hh
    int time1_m; //mm
    int time2_h; //hh
    int time2_m; //mm
    char* appointment;
}Schedule;

void Split_line(FILE* ifp, int schedule_num, Schedule *schedule);
void Split_time(int schedule_num, Schedule *schedule);

int main(void)
{
    FILE *ifp = NULL;
    int schedule_num = 0;
    int i;
    Schedule *schedule;

    fopen_s(&ifp, "C:\\sample.txt", "r");
    fscanf(ifp, "%d\n", &schedule_num);

    schedule = (Schedule*)malloc(sizeof(Schedule)*schedule_num);
    for (i = 0; i < 4; i++) {
        schedule[i].time1 = (char*)malloc(sizeof(char) * 50);
        memset(schedule[i].time1, 0, 50);
        schedule[i].time2 = (char*)malloc(sizeof(char) * 50);
        memset(schedule[i].time2, 0, 50);
        schedule[i].appointment = (char*)malloc(sizeof(char) * 50);
        memset(schedule[i].appointment, 0, 50);

    }
    Split_line (ifp, schedule_num, schedule); // Split each line into time1, time2, and attachment
                                             //Split_time(schedule_num, schedule); // Split time1, time2 of each line
}

void Split_line(FILE* ifp, int schedule_num, Schedule *schedule) {

    int i, l = 0;
    char buff[255];
    char* tmp;

    printf("%d\n", schedule_num);

    for (i = 0; i<schedule_num; i++) {
        //printf("%d\n", i);
        fgets(buff, 255, (FILE*)ifp); // Read each line
        tmp = strtok(buff, "); //time1 Detach
        strcat(schedule[i].time1, tmp);
        tmp = strtok(NULL, "); //time2 Detach
        strcat(schedule[i].time2, tmp);
        tmp = strtok(NULL, "\n"); //appointments to disconnect
        strcat(schedule[i].appointment, tmp);

        printf("%s %s %s\n", schedule[i].time1, schedule[i].time2, schedule[i].appointment);
        /*for (l = 0; l <= i; l++) {
            printf("%s %s %s\n", schedule[l].time1, schedule[l].time2, schedule[l].appointment);
        }*/
    }
    printf("\nCheck\n");
    printf("%s %s %s\n", schedule[0].time1, schedule[0].time2, schedule[0].appointment);
    printf("%s %s %s\n", schedule[1].time1, schedule[1].time2, schedule[1].appointment);
    printf("%s %s %s\n", schedule[2].time1, schedule[2].time2, schedule[2].appointment);
    printf("%s %s %s\n", schedule[3].time1, schedule[3].time2, schedule[3].appointment);
    fclose(ifp);

}


2022-09-22 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.