Question: create a function read_words() that takes in one argument, a file pointer, and then reads in words from that file into an array of words.

create a function read_words() that takes in one argument, a file pointer, and then reads in words from that file into an array of words.

-There will be at least one word per line. Other than that, the words in this file may be placed in any order (it will definitely not be ordered alphabetically) and can be any number of words.

- For reading in a word from the file you may use a local character array of fixed size

-You can assume that the longest word in English has 45 characters in it

-The maximum number of words that might be stored in the array of words can be computed from the file size (as opposed to assuming a maximum number as a constant).

-As there can be no more words in a file then number of bytes in the file, use file_size(fp) to calculate the max number of words possibly in that file

-Note: this is an overestimate: for a single character to be a separate word you need a space or newline after the character ... so the maximum word count is actually half the byte size of the file

-The word array should also hold a NULL pointer after the entered words to indicate the end of the word list

- The array of words, once created, should be returned from the function

main:

#include

#include

#include

int main (int argc, char* argv[]){

FILE *fp;

char a[500];

if ((fp = fopen("a1_words.txt", "r")) != NULL){ /*opening and reading a file*/

file_size(fp);

printf("%d ", *fp);

a[1] = read_words(&fp);

printf("%s ", a);

}

else {

printf("File Not Found ");

return -1;

}

function:

#include

char read_words(FILE *fp){

char ch[100];

int count = 0;

char c;

fp = fopen("a1_words.txt", "r");

while (c = fgetc(fp) != EOF){

ch[count] = c;

if (c == ' ' || c == ' '){

count++;

}

}

/*while (c = fgets(stdin, 1000, fp) != NULL){

ch[count] = c;

if (c == " " || c == ' '){

count++;

}

}*/

return *ch;

}

read_words(fp);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!