Write a complete (with all necessary declaration) function called loadMyAddressB
ID: 674194 • Letter: W
Question
Write a complete (with all necessary declaration) function called loadMyAddressBook that:
1. requires an argument as a char pointer to the file name.
2. The function must open the file names in the argument for reading.
3. Must insure that the file was opened succesfully.
4. Must read the file line by line useing "fgets" storing the read data in char awway called myLine assuming that no line is longer than 80 characters (some may be exactly 80 characters long).
5. Must continue reading until it encounters the end of file.
(Hint: use the following syntax for fopen and fget):
FILE * fopen ( const char * filename, const char * mode );
char * fgets ( char * str, int num, FILE * stream);
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void loadMyAddressBook(char *addrBookPt);
int main()
{
loadMyAddressBook("myaddrbook.txt");
return 0;
}
void loadMyAddressBook(char *addrBookPtr)
{
FILE *addrBookFile;
char fileLine[81];
addrBookFile = fopen(addrBookPtr, "r");
// If the fopen returns null, let's return now
if(addrBookFile == NULL)
{
return;
}
// While fgets is NOT at the EOL
while(fgets(fileLine, sizeof fileLine, addrBookFile) != NULL)
{
printf("%s", fileLine);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.