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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started