개발

C언어 - save && load 예제

Swimming_Kim 2019. 2. 4. 21:46

파일 입출력 쓸 때 보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void load(FILE *fp) {
    char buffer[BUFFER_LENGTH];
    char *name, *title, *path, *context;
 
    while (1) {
        if (read_line(fp, buffer, BUFFER_LENGTH) <= 0)
            break;
        name = strtok_s(buffer, "#"&context);
        if (strcmp(name, " "== 0)
            name = NULL;
        else
            name = _strdup(name);
 
        title = strtok_s(NULL"#"&context);
        if (strcmp(title, " "== 0)
            title = NULL;
        else
            title = _strdup(title);
 
        path = strtok_s(NULL"#"&context);
        if (strcmp(path, " "== 0)
            path = NULL;
        else
            path = _strdup(path);
 
        add_song(name, title, path);
    }
}
 
void save_song(Song* ptr_song, FILE *fp){
    if(ptr_song->artist != NULL)
        fprintf(fp, "%s#", ptr_song->artist->name);
    else 
        fprintf(fp, " #");
    if(ptr_song->title != NULL)
        fprintf(fp, "%s#", ptr_song->title);
    else
        fprintf(fp, " #");
    if (ptr_song->path != NULL)
        fprintf(fp, "%s#\n", ptr_song->path);
    else
        fprintf(fp, " #\n");
}
cs