Last active
October 10, 2023 15:53
-
-
Save maq796113/b6cab7d7d1950576d25b1e87b4c15c37 to your computer and use it in GitHub Desktop.
Read Text File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
bool isTerminator(char ch) { | |
bool result = false; | |
char terminators[5] = { ';', '.', '!' , '?'}; | |
for (int j = 0; j < sizeof(terminators) / sizeof(terminators[0]); j++) { | |
if (ch == terminators[j]) { | |
result = true; | |
break; | |
} | |
} | |
return result; | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
printf("No arguments were provided, try again and kindly provide valid argument(s)"); | |
exit(0); | |
} | |
FILE* fptr; | |
fptr = fopen(argv[1], "r"); | |
if (fptr == NULL) { | |
printf("Cannot open file"); | |
exit(0); | |
} | |
char c; | |
int i = 0; | |
while ((c = fgetc(fptr)) != EOF) { | |
if (c == '\n') { //check if there is a new line | |
printf(" ---- %d\n", i); | |
i = 0; | |
continue; | |
} | |
if (c == 32) { | |
if (i == 0) { //if the first character of the line is whitespace then skip to the part where we there is no longer a white space | |
while ((c = fgetc(fptr)) == 32); | |
} | |
else { //in the case the whitespace is between two words or before a terminator, period ,etc. | |
while ((c = fgetc(fptr)) == 32); //skip all the whitespaces | |
if (!isTerminator(c)) { //in case the character after all the whitespaces is not a terminator, persumably it's going to be between words | |
printf(" "); | |
} | |
} | |
} | |
printf("%c", c); | |
i++; | |
} | |
fclose(fptr); | |
return 0; | |
} | |
//whenever you call fgetc() with the same filepointer, it gives you the next character found the file. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Problem is As Follows:
Your program should get one input from user on command line. The argument should be the name of a file available on your PWD. Make sure that user has provided correct inputs. First argument must be name of a file available in the PWD. Any incorrect or invalid input should result the program to terminate with an appropriate message to the user.
Open the file in read mode. Ensure that file must exist in the PWD.
Your program should read the file character by character and display it on the console.
While reading the file, program should eliminate ALL unnecessary spaces encountered.
All single spaces that are there within the words in a line are considered as necessary spaces.
An unnecessary space can be defined as:
a. All space/s in the beginning of a line before any character. It also includes tabs.
b. Any space or spaces in the end of the line or before a semicolon, colon, comma or full stop.
c. If there are multiple spaces within the words, your program MUST eliminate all but one space.
Suppose the input file is as follows:
Hello there . How are you ?
This is a stateme nt. char myvar = ’a’ ;
and you compile the program as:
$ gcc lab3.c -o lab3
$ ./lab3 inFile
Output file is:
Hello there. How are you?
This is a stateme nt. char myvar = ’a’