In C language, write the following two functions maintaining a database of entries of type song in a file. Here is the definition of the struct song: typedef struct { char* title; char* artist; int year; } song; int add_song(const char* file_name, const song s); 1. The function gets the name of a file, and a song. If the song is not in the file, the function adds it to the file and returns 1. Otherwise, the function does not modify the file and returns 0. song* find_song(const char* file_name, const char* title); 2. The function gets the name of a file and a title of a song. It searches the file for the song with the given title. If a song is found, it returns a pointer to the song with all the details. If not found, the function returns NULL. You may assume the song titles are unique. Additional instructions and hints: 1. For the instructions on how to read and write to files see section "C Programming Files" in https://www.programiz.com/c-programming or https://www.tutorialspoint.com/cprogramming/c_file_io.htm or any other online resources. 2. There are no specific instructions about how you should store the information in the file. The only requirement is that the two functions are compatible with each other. That is, if a song is added using add_song, then find_song will be able to find it. You should decide carefully on the format for storing the data of each song. 3. When storing the title and artists, remember that you need to store the actual string and not the pointer to it. Also, it may be convenient to store the length of the string in the file. 4. Don't forget to close the file at the end of each function. Example test for function 1: void test_q1() { song bd = {"All Along the Watchtower", "Bob Dylan", 1968}; song jj = {"Mercedes Benz", "Janis Joplin", 1971}; song lz = {"Stairway to Heaven", "Led Zeppelin", 1971}; int tmp; tmp = add_song("songs.db", bd); if(tmp ==1) printf("Q4-1 ok\n"); else printf("Q4-1 ERROR : expected 1, return = %d\n", tmp); Example test for function 2: void test_q1() { song bd = {"All Along the Watchtower", "Bob Dylan", 1968}; song jj = {"Mercedes Benz", "Janis Joplin", 1971}; song lz = {"Stairway to Heaven", "Led Zeppelin", 1971}; song* s_mb = find_song("songs.db", "Mercedes Benz"); if(s_mb && strcmp(s_mb->artist,"Janis Joplin")==0) printf("Q4-5 ok\n"); else printf("Q4-5 ERROR, Mercedes Benz not found correctly\n"); if(s_mb) free(s_mb);
In C language, write the following two functions maintaining a
int add_song(const char* file_name, const song s);
1. The function gets the name of a file, and a song. If the song is not in the file, the function adds it to the file and returns 1. Otherwise, the function does not modify the file and returns 0.
song* find_song(const char* file_name, const char* title);
2. The function gets the name of a file and a title of a song. It searches the file for the song with the given title. If a song is found, it returns a pointer to the song with all the details. If not found, the function returns NULL. You may assume the song titles are unique.
Additional instructions and hints:
1. For the instructions on how to read and write to files see section "C Programming Files" in https://www.programiz.com/c-programming or https://www.tutorialspoint.com/cprogramming/c_file_io.htm or any other online resources.
2. There are no specific instructions about how you should store the information in the file. The only requirement is that the two functions are compatible with each other. That is, if a song is added using add_song, then find_song will be able to find it. You should decide carefully on the format for storing the data of each song.
3. When storing the title and artists, remember that you need to store the actual string and not the pointer to it. Also, it may be convenient to store the length of the string in the file.
4. Don't forget to close the file at the end of each function.
Example test for function 1:
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images