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.
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.
-
Answer # 1
-
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];
Related articles
- c language: method that does not output 0 in loop output using array
- pointer reference in c language structure
- a method to read a character string from a file in c language and digitize a specific part
- file output in multithread in c language
- c language output function printf variable usage problem
- about c language list structure
- i want to output in c language without using strlen
- about c language character string operation
- about c language structure, typedef
- c language, if statement structure
- please tell me how to output the character string
- string exchange using c language pointers
- c language character string encryption
- change the pointer of the c language string to an empty string
- c language character string display
- c language character string scanning
- c language output of numbers in the specified range and number of lines
- c language: i want to copy a character to the string literal passed as an argument
- 3b the character string read from the file cannot be output in reverse order
- (c language) i want to output the bbox coordinate data detected by yolo v3 to a txt file
char year [5]
and 5 bytes are reserved, butsscanf ()
separates2019
Insert 7byte (including null characters). * Character code is based on SJISRead both month and day as well as secured.