The main function may only declare variables, call functions, and manage functio
ID: 3635179 • Letter: T
Question
The main function may only declare variables, call functions, and manage function execution using control structures.
Create one function to prompt the user, get the file name, and return an open file pointer
Create one function to accept an open file pointer and return an array of structs with the unique words and their line number reference
Create one function to accept the array of structs created in (3) and display the sorted output to the user
You may create and use any additional functions you require
Rewrite this Program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define fileNameLength 256
typedef struct {
char word[256];
int lineNumber;
} word;
int main()
{
FILE *inputFile; // file pointer
char inputFileName[256] = {0};
int fileLine = 0;
int wordsCountedSoFar = 0;
int i, j;
char bufferWord[256];
char bufferChar;
char *result = NULL;
char delims[] = " ";
int foundWord = 0;
word Dictionary[1000]; // array of 1000 word structure objects
printf("Program: Structs ");
do
{
printf("Enter an input file name: ");
fgets(inputFileName, fileNameLength, stdin);
inputFileName[strlen(inputFileName) - 1] = '';
if ((inputFile = fopen(inputFileName, "r")) == NULL)
{
printf("Unable to open "%s": %s ", inputFileName,
strerror(errno));
}
} while (inputFile == NULL);
for(i = 0; i < 1000; ++i)
{
Dictionary[i].lineNumber = 0;
}
while (fgets(bufferWord, 256, inputFile) )
{
++fileLine;
foundWord = 0;
bufferWord[strlen(bufferWord) -1] = '';
result = strtok( bufferWord, delims );
while( result != NULL ) {
//printf( ""%s" ", result );
for(i = 0; i < wordsCountedSoFar; ++i)
{
// if word is found in the dictionary
if(strcmp(Dictionary[i].word, result) == 0)
{
foundWord = 1;
break;
}
// places in the dictionary in alphabetical order
else if(strcmp(Dictionary[i].word, result) > 0)
{
// shift one position right
for(j = wordsCountedSoFar; j >= i; j--)
{
strcpy(Dictionary[j+1].word, Dictionary[j].word);
Dictionary[j+1].lineNumber = Dictionary[j].lineNumber;
}
break;
}
}
// checking if the word is already in the structure array
if(foundWord == 0)
{
strcpy(Dictionary[i].word, result);
wordsCountedSoFar += 1;
Dictionary[i].lineNumber = fileLine;
}
//store the distinct words
result = strtok( NULL, delims );
}
}
// closes the file
fclose(inputFile);
// displays the structures array
printf(" The input file contains %d distinct words ", wordsCountedSoFar);
for(i = 0; i < 1000; ++i)
{
if(Dictionary[i].lineNumber > 0)
printf(" word-> %s found at line: %d", Dictionary[i].word, Dictionary[i].lineNumber);
}
printf(" ");
return 0;
}
Explanation / Answer
Dear,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define fileNameLength 256
typedef struct {
char word[256];
int lineNumber;
} word;
/* reads file name from the console,
opens the file and returns the filr pointer */
FILE * readFileName()
{
FILE *inputFile;
char inputFileName[256] = {0};
do
{
printf("Enter an input file name: ");
fgets(inputFileName, fileNameLength, stdin);
inputFileName[strlen(inputFileName) - 1] = '';
if ((inputFile = fopen(inputFileName, "r")) == NULL)
{
printf("Unable to open "%s": %s ", inputFileName,
strerror(errno));
}
} while (inputFile == NULL);
return inputFile;
}
/* Reads data into the Dictionary from the input file
and returns number of distinct words found in the file */
int readData(FILE *inputFile, word Dictionary[])
{
int i, j;
int fileLine = 0;
char bufferWord[256];
char *result = NULL;
char delims[] = " ";
int foundWord = 0;
int wordsCountedSoFar = 0;
for(i = 0; i < 1000; ++i)
{
Dictionary[i].lineNumber = 0;
}
while (fgets(bufferWord, 256, inputFile) )
{
++fileLine;
foundWord = 0;
bufferWord[strlen(bufferWord) -1] = '';
result = strtok( bufferWord, delims );
while( result != NULL ) {
for(i = 0; i < wordsCountedSoFar; ++i)
{
// if word is found in the dictionary
if(strcmp(Dictionary[i].word, result) == 0)
{
foundWord = 1;
break;
}
// places in the dictionary in alphabetical order
else if(strcmp(Dictionary[i].word, result) > 0)
{
// shift one position right
for(j = wordsCountedSoFar; j >= i; j--)
{
strcpy(Dictionary[j+1].word, Dictionary[j].word);
Dictionary[j+1].lineNumber = Dictionary[j].lineNumber;
}
break;
}
}
// checking if the word is already in the structure array
if(foundWord == 0)
{
strcpy(Dictionary[i].word, result);
wordsCountedSoFar += 1;
Dictionary[i].lineNumber = fileLine;
}
//store the distinct words
result = strtok( NULL, delims );
}
}
// closes the file
fclose(inputFile);
return wordsCountedSoFar;
}
/* Displays the dictionary data on the console */
void displayData(word Dictionary[], int wordsCountedSoFar)
{
int i;
for(i = 0; i < 1000; ++i)
{
if(Dictionary[i].lineNumber > 0)
printf(" word-> %s found at line: %d", Dictionary[i].word, Dictionary[i].lineNumber);
}
}
/* main function */
int main()
{
FILE *inputFile; // file pointer
int wordsCountedSoFar = 0;
word Dictionary[1000]; // array of 1000 word structure objects
printf("Program: Structs ");
inputFile = readFileName();
wordsCountedSoFar = readData(inputFile, Dictionary);
// displays the structures array
printf(" The input file contains %d distinct words ", wordsCountedSoFar);
displayData(Dictionary, wordsCountedSoFar);
printf(" ");
return 0;
}
I hope this will helps you...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.