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

( C language). Write a complete (with all necessary declaration) function called

ID: 3808112 • Letter: #

Question

( C language). 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

#include <stdio.h>

void loadMySongsList(char *fileName)
{
    char songBuff[133]; //132 characters for song Name & 1 charcter for ''
    FILE *file;
    file = fopen(fileName, "r"); //read file in read mode
    if (file) { // check file opened sucessfully
        while(fgets(songBuff, 132, file)) {
            printf("%s ", songBuff);
        }
        fclose(file);
    }
    else { //In case file read failed
        printf("File reading failed");
    }
}
int main() {
    char * fileName= "songs.txt";
    loadMySongsList(fileName);    
    return 0;
}