2017-12-01 20 views
0

노래 데이터베이스를 코딩 중입니다. 지금은 2 곡의 데이터가 한 줄에 하나의 구조 필드 인 텍스트 파일로 저장됩니다. 나는 줄 단위로 파일의 내용을 배열에 복사하고 싶습니다. load()를 호출 한 후 왜 프로그램이 충돌하는지 알 수 없습니다. fgets()와 관련된 문제입니까? 또는 '\ n'을 '\ 0'으로 바꿀 때?C의 배열에 한 줄씩 파일 내용로드

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 
#include "functions.h" 

int main() 
{ 
    int menu; 
    bool exit = false; 
    /*char title[256]; 
    char artist[256]; 
    char album[256];*/ 
    int year; 
    Song **songs = NULL; // base pointer to the array (of pointers to struct) 
    FILE *f; // pointer to a file structure 
    int n = 0; // height of array at the beginning (= number of pointers to struct) 
    int i; 

    f = fopen("database.txt", "r+"); 
    if(f == NULL) 
     return 0; 

    count_songs_db(f, &n); // will modify n (height of array) according to the number of existing songs 
    printf("n = %d\n", n); 

    songs = (Song**)malloc(n*sizeof(Song)); 
    load(f, songs, n); 

    // MENU 

    for(i = 0 ; i < n ; ++i) 
     free(songs[i]); 

    free(songs); 
    fclose(f); 

    return 0; 
} 

기능 :

void count_songs_db(FILE *f, int *n) // calculate how many songs there are already in the database. 
{ 
    int c, n_lines = 0; 

    while ((c = getc(f)) != EOF) 
     if (c == '\n') 
      ++n_lines; // count number of lines 
    *n = n_lines/6; // 1 song = 6 lines. Changes the height of array accordingly. 
    rewind(f); // go back to beginning of file, to be able to load the db 
} 

void load(FILE *f, Song **songs, int n) // load existing songs (in the file) into the array 
{ 
    int i; 

    for(i = 0 ; i < n ; ++i) 
    { 
     fgets(songs[i]->title, 256, f); // reads a line of text 
     songs[i]->title[strlen(songs[i]->title)-1] = '\0'; // to replace \n by \0 at the end // not working? 
     fgets(songs[i]->artist, 256, f); 
     songs[i]->title[strlen(songs[i]->artist)-1] = '\0'; 
     fgets(songs[i]->album, 256, f); 
     songs[i]->title[strlen(songs[i]->album)-1] = '\0'; 
     fscanf(f, "%d\n", &(songs[i]->year)); // use it like scanf 
     fgets(songs[i]->genre, 256, f); 
     songs[i]->title[strlen(songs[i]->genre)-1] = '\0'; 
     fscanf(f, "%d:%d\n", &(songs[i]->length.m), &(songs[i]->length.s)); 
    } 

    for(i = 0 ; i < n ; ++i) 
    { 
     printf("Title: %s\n", songs[i]->title); 
     printf("Artist: %s\n", songs[i]->artist); 
     printf("Album: %s\n", songs[i]->album); 
     printf("Year of release: %d\n", songs[i]->year); 
     printf("Genre: %s\n", songs[i]->genre); 
     printf("Length: %d:%d\n", songs[i]->length.m, songs[i]->length.s); 
    } 
} 

구조체 : 당신의 도움에 대한

typedef struct Length { 
int m, s; 
} Length; 

typedef struct Song { 
    char title[256]; 
    char artist[256]; 
    char album[256]; 
    int year; 
    char genre[256]; 
    Length length; 
} Song; 

감사 다음은 코드의 흥미로운 부분입니다.

편집 : 구조체의 간단한 배열을 사용하도록 코드를 수정했습니다. 여기에 add_song() 함수는 저장() 함수 :

void add_song(Song *songs, int *n) 
{ 
    printf("Title: "); 
    read(songs[*n].title, MAX_SIZE); // another function is used instead of scanf(), so the user can enter string with spaces. Also more secure. 
    printf("Artist: "); 
    read(songs[*n].artist, MAX_SIZE); 
    printf("Album: "); 
    read(songs[*n].album, MAX_SIZE); 
    printf("Year of release: "); 
    songs[*n].year = read_long(); // still have to check the user inputs (ie. year has to be between 1900 and 2017) 
    printf("Genre: "); 
    read(songs[*n].genre, MAX_SIZE); 
    printf("Length: \nmin: "); 
    songs[*n].length.m = read_long(); 
    printf("sec: "); 
    songs[*n].length.s = read_long(); 

    ++(*n); 
} 

void save(FILE *f, Song *songs, int n) // save song in file 
{ 
    fprintf(f, "%s\n%s\n%s\n%d\n%s\n%d:%d\n", songs[n-1].title, songs[n-1].artist, songs[n-1].album, songs[n-1].year, songs[n-1].genre, songs[n-1].length.m, songs[n-1].length.s); // use it like printf. Prints the data in the file. 
} 
+2

'송 ** 노래 = NULL;'와'노래 = (송 **) malloc에 ​​(n 개의 *의를 sizeof (노래)); '일치하지 마. 'Song * songs = NULL;로 변경하고 다른 곳에서'노래 [i] -> 제목을'노래 [i] .title' 등으로 변경하십시오 ... –

+0

'malloc' 결과를 캐스팅해서는 안됩니다. C에서 - 올바른 포인터 유형으로 안전하게 승격되고 캐스팅은 컴파일러 오류를 가릴 수 있습니다. –

+0

텍스트 파일은 ascii 인코딩으로 저장해야 하나의 char = 1 Byte가되며 항상이 라인 노래 [i] -> title [strlen (노래 [i] -> album) -1] = '\ 0'아티스트 [], 앨범 [] 및 장르 []에 대해 동일한 작업을 수행하는 것을 잊었습니다. –

답변

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


typedef struct Length { 
    int m, s; 
} Length; 

typedef struct Song { 
    char title[256]; 
    char artist[256]; 
    char album[256]; 
    int year; 
    char genre[256]; 
    Length length; 
} Song; 
void count_songs_db(FILE *f, int *n) // calculate how many songs there are already in the database. 
{ 
    int c, n_lines = 0; 

    while ((c = getc(f)) != EOF) 
     if (c == '\n') 
      ++n_lines; // count number of lines 
    *n = n_lines/6; // 1 song = 6 lines. Changes the height of array accordingly. 
    rewind(f); // go back to beginning of file, to be able to load the db 
} 

void load(FILE *f, Song *songs, int n) // load existing songs (in the file) into the array 
{ 
    int i; 

    for (i = 0; i < n; ++i) 
    { 
     fgets(songs[i].title, 256, f); // reads a line of text 
     songs[i].title[strlen(songs[i].title) - 1] = '\0'; // to replace \n by \0 at the end // not working? 
     fgets(songs[i].artist, 256, f); 
     songs[i].title[strlen(songs[i].artist) - 1] = '\0'; 
     fgets(songs[i].album, 256, f); 
     songs[i].title[strlen(songs[i].album) - 1] = '\0'; 
     fscanf(f, "%d\n", &(songs[i].year)); // use it like scanf 
     fgets(songs[i].genre, 256, f); 
     songs[i].title[strlen(songs[i].genre) - 1] = '\0'; 
     fscanf(f, "%d:%d\n", &(songs[i].length.m), &(songs[i].length.s)); 
    } 

    for (i = 0; i < n; ++i) 
    { 
     printf("Title: %s\n", songs[i].title); 
     printf("Artist: %s\n", songs[i].artist); 
     printf("Album: %s\n", songs[i].album); 
     printf("Year of release: %d\n", songs[i].year); 
     printf("Genre: %s\n", songs[i].genre); 
     printf("Length: %d:%d\n", songs[i].length.m, songs[i].length.s); 
    } 
} 

int main() 
{ 
    int menu; 
    bool exit = false; 
    /*char title[256]; 
    char artist[256]; 
    char album[256];*/ 
    int year; 
    Song *songs = NULL; // base pointer to the array (of pointers to struct) 
    FILE *f; // pointer to a file structure 
    int n = 0; // height of array at the beginning (= number of pointers to struct) 
    int i; 

    f = fopen("database.txt", "r+"); 
    if (f == NULL) 
     return 0; 

    count_songs_db(f, &n); // will modify n (height of array) according to the number of existing songs 
    printf("n = %d\n", n); 

    songs = (Song*)malloc(n * sizeof(Song)); 
    load(f, songs, n); 

    // MENU 

    free(songs); 

    fclose(f); 

    return 0; 
} 
+0

정말 고맙습니다. 실제로해야 할 일이었습니다. 코드를 편집했지만 이제 add_song() 함수에 문제가 발생합니다. 예를 들어 이미 2 곡이 이미있는 경우 위치 노래 [2]에 노래를 추가하고 싶습니다. – Parallel