Home>
Problem i am experiencing

The date and the schedule of the day written in the following schedule.txt
(Example) December 25, 2019 Christmas
I want to put it in a structure and output it, but it doesn't work.

Preschedule.txt contents
December 2, 2019 Submission
December 3, 2019 Midterm test
December 25, 2019 Christmas
Main function
#include<stdio.h>
struct schedule
{
    char year [5];
    char month [3];
    char day [3];
    char content [9999];
};
int main ()
{
    FILE * fp;
    char str [9999];
    struct schedule schedules [3];
    fp = fopen ("preschedule.txt", "r");
    if (fp == NULL)
    {
       printf ("File does not exist. \ n");
    }
    while (fgets (str, sizeof (str), fp)! = NULL)
    {
        int i = 0;
        sscanf (str, "% s% s% s% s", schedules [i] .year, schedules [i] .month, schedules [i] .day, schedules [i] .content);
        printf ("% s% s% s% s \ n", schedules [i] .year, schedules [i] .month, schedules [i] .day, schedules [i] .content);
        i ++;
    }
    fclose (fp);
    }
Output results
2019-12-2-Submission of tasks-12-2-Submission of issues-2-Submission of issues-Submission of issues
2019 ▒ 12 ▒ 3 ▒ Mid-term test 12 ▒ 3 ▒ Mid-term test 3 ▒ Mid-term test Mid-term test
2019▒12▒25▒Christmas 12▒25▒Christmas 25▒Christmas Christmas
Problem

As shown in the output result above, there are parts that are garbled, or more characters than schedules [i] .year, schedules [i] .month, schedules [i] .day, schedules [i] .content. Please give me a solution to this problem.

c
  • Answer # 1

    char year [5]and 5 bytes are reserved, butsscanf ()separates2019Insert 7byte (including null characters). * Character code is based on SJIS
    Read both month and day as well as secured.

  • Answer # 2

    What is OS and compiler?
    If you are using gcc on Linux, the character code is usually UTF-8.

    For Linux, see $hex preschedule.txt for a hex dump.
    000000 32 30 31 39 e5 b9 b4 20 31 32 e6 9c 88 20 32 e6>2019 ... 12 ... 2.<
    000010 97 a5 20 e8 aa b2 e9 a1 8c e6 8f 90 e5 87 ba 0a>.. .............<
    The "year" code is 3 bytes of e5 b9 b4.

    Char year [8], month [6], day [6];