Objective
The program reads a file containing song information, stores the details in dynamically allocated structures, and prints the song details of every 5 songs starting from first song. The song information includes the genre, artist, and title of each song.
Requirements
- File Format
- Song Struct
- Song struct holds a single song's information
- It is made up of 3 c-strings of varying lengths
#define MAX_GENRE_LENGTH 30
#define MAX_ARTIST_LENGTH 50
#define MAX_TITLE_LENGTH 100
typedef struct Song {
char genre[MAX_GENRE_LENGTH];
char artist[MAX_ARTIST_LENGTH];
char title[MAX_TITLE_LENGTH];
} Song;
- Remove unwanted characters from end of line:
remove_char_from_end(char * str, char char_to_remove)
- Check if provided c-string ends withe char_to_remove
- If so, remove the last character from the c-string
- Read and Load Songs:
Song* read_songs_from_file(const char *filename, int *count)
- Open file using provided filename
- Allocate initial memory for songs, capacity = 10, don't forget sizeof()
- While
getline
(using getline, which auto allocates on the heap any size of input)
- If +++BEGIN check
- If song_index is >= capacity, then allocate more space for list
- Use memset to set all the strings in the struct to 0 for the current array location
- If unsure about memset, then watch this video on memset
- Clean up line using remove_char_from_end
- If genre: artist: title: in line then
- Copy the value to the structure using strncpy
- Add null terminator to the end of the field (Use MAX value) b/c strncpy does not guarantee null termination when value is longer than genre's max size
- Set count pointer equal to
song_index + 1
- Close file and free line
- The main function
- Check that file argument was provided otherwise print usage message and exit
- Call
read_songs_from_file
- Print every fifth song from the heap-based array
- Free songs
Steps to Complete
- Implement the requirements
- Compile and test the program
- Run /challenge/tester
- Get flag