Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programing Write a complete (with all necessary declaration) function called l

ID: 3808506 • Letter: C

Question

C programing

Write a complete (with all necessary declaration) function called loadMySongsList that: b. requires an argument as a char pointer to the file name. c. The function must open the file named in the argument for reading, d. Must insure that the file was opened successfully. e. Must read the file line by line using "fgets" storing the read data in char array called songBuff assuming that no song name is longer that 132 characters (some may be exactly 132 characters long) f. Must continue reading until it encounters the end of file. g. You must include a main that calls the loadMySongsList and display the results. h. Your program must not cause any memory leaks

Explanation / Answer

Executable code:

#include <stdio.h>

void loadMySongsList(const char *p) {
int i;
char songBuff[133];
FILE *myfile;
myfile = fopen(p, "r");
if (myfile == NULL) {
printf ("Error, file couldn't be opened. ");
}
else {
while (1) {
if ((fgets(songBuff, 133, myfile)) == NULL) {
break;
}

  
i = 0;
while (songBuff[i] != '') {
printf("%c", songBuff[i]);
i++;
}
printf (" ");
}
printf (" Loaded the songs list successfully!");
fclose(myfile);
}
}

int main() {
const char filename[] = "listfile.txt";
printf(" ");
printf("---------------------------------------- ");
printf(" Testing the function ");
printf("---------------------------------------- ");
printf(" ");
loadMySongsList(filename);
printf(" ");
return 0;
}