Description: The purpose of this assignment is to review file processing, strtok
ID: 3676961 • Letter: D
Question
Description:
The purpose of this assignment is to review file processing, strtok() and cstrings. In this assignment, you need to create a simple word processor, which reads an input file, processes each line and writes it back to an output file.
Preparation:
Review file processing, strtok(), strcat() and other cstring operations.
Instructions:
The source file for this assignment will be named Lab6_<username>.c
When running your program, your program should prompt the user to enter the name of an input file containing text ("Lab6_bad.txt”), and then prompt you for the name of an output file, such as “Lab6_good.txt”.
Then, your program will open the input file for reading, and open the output file for writing. The program reads the input file, one line at a time. After each line is read, the line will be printed to the screen and a prompt will appear asking the user which word (as a position in the sentence) should be removed from the line. For instance, the program might print the following sentence and prompt:
You are to NARF! create a (very) simple word processor.
Enter position of word to delete (Start counting at 0). Enter -1 to skip deletion:
The user will enter the value 3 to remove "NARF!" from the sentence (remember that the first word is in position 0).
The program will then tokenize the words in the sentence using strtok(), and create a new sentence without the chosen word using strcat(). It will then write this new sentence to the output file. The removed word for each sentence will be saved in a separate array, which will be printed out at the end of the program.
Your program continues in the above fashion until all the sentences in the original input file have been processed. After all of the sentences have been processed, close the output file and write the list of words that were deleted from each line (saved in a separate array as described in the above paragraph) to stdout(the screen).
Note that your program should be able to handle sentences where the user does not wish to remove a word. The user will indicate that they do not wish to remove a word by entering a negative value.
You may assume that the file will contain less than 100 lines, and that each line in the file has less than 101 characters (including the null terminator). Remember to close both the input and output files before exiting your program.
Be sure to include comments within your code that explain in high-level terms what the various parts of your code are doing. Also, include pre-conditions and post-conditions as appropriate. In addition, the output file “Lab6_good.txt” should be exactly like “Lab6_bad.txt” with the deleted words having been removed.
Lab6_bad.txt:
Explanation / Answer
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char inFileName[20];
char outFileName[20];
printf("Enter input file: ");
scanf("%s", inFileName);
printf("Enter output file: ");
scanf("%s", outFileName);
FILE* file = fopen(inFileName, "r");
FILE* outfile = fopen(outFileName, "w");
char line[256];
char *token;
char *word;
char skipWords[50][20];
char delim[] = " ,.!;: "; /* sets up word separators*/
int skipCount = 0;
while (fgets(line, sizeof(line), file)) {
char newline[256] = {''};
/* note that fgets don't strip the terminating , checking its
presence would allow to handle lines longer that sizeof(line) */
printf("%s", line);
token = line;
int count =0;
int pos = 0;
// reading position
printf("Enter a position of word: ");
scanf("%d",&pos);
//tokenizing string
while ((word = strtok(token, delim)) !=NULL)
{
// if postion of word is equal to user input
if(count == pos){
strcpy(skipWords[skipCount++], word);// skip
}
else{// add in new line
strcat(newline , word);
strcat(newline , " ");
}
// printf(" %s ",word);
// printf(" %s ",newline);
token = NULL;
count++;
}
// putting into output file
fputs(newline, outfile);
putc(' ', outfile);
}
fclose(file);
//printing skipped words
printf(" Skipped words are: ");
int k;
for(k=0; k<skipCount; k++){
printf("%s, ",skipWords[k]);
}
return 0;
}
/*Output:
Enter input file: Lab6_bad.txt
Enter output file: Lab6_good.txt
You are to NARF! create a (very) simple word processor.
Enter a position of word: 3
Your program will PINKY prompt the user to enter the name
Enter a position of word: 3
of an input file BRAIN containing text ("Lab4_bad.txt").
Enter a position of word: 4
It will then prompt you for the name TAKE of an output file.
Enter a position of word: 8
Your program OVER will open the input file for reading,
Enter a position of word: 2
and open the output file for THE writing. The program
Enter a position of word: -1
will then read the input file, one line at a time.
Enter a position of word: -1
After each line is WORLD! read, the line will be printed to the
Enter a position of word: 4
screen and a TONIGHT prompt will appear.
Enter a position of word: 3
Skipped words are:
NARF, PINKY, BRAIN, TAKE, OVER, WORLD, TONIGHT,
//content of Lab6_good.txt
You are to create a (very) simple word processor
Your program will prompt the user to enter the name
of an input file containing text ("Lab4_bad txt")
It will then prompt you for the name of an output file
Your program will open the input file for reading
and open the output file for THE writing The program
will then read the input file one line at a time
After each line is read the line will be printed to the
screen and a prompt will appear
*/