Chapter - 13: File Input-Output

Given a text file, write a program to create another text file deleting the words "a", "the", "an" and replacing each one of them with a blank space.


C
Sections
14
Exercises

A

B

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

File (n).txt

Every game is a part of my life and every part of my life is a game.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

#define FOUND (strcmp(word, "a") == 0 || strcmp(word, "an") == 0\
|| strcmp(word, "the") == 0 || strcmp(word, "A") == 0\
|| strcmp(word, "An") == 0 || strcmp(word, "The") == 0)

void delart(char*);

int main()

{
	delart("Temp.txt");
	_getch();
	return 0;
}

void delart(char *file)
{
	FILE *fp, *nf;
	int i;
	char word[20];
	fp = fopen(file, "r+");
	nf = fopen("NEW FILE.txt", "w");
	if (fp == NULL)
	{
		puts("\nCannot open the file.\n");
		exit(1);
	}
	for (i = 0; 1; i++)
	{
		word[i] = fgetc(fp);
		if (word[i] == EOF)
			break;
		if (word[i] == ' ')
		{
			word[i] = '\0';
			if (FOUND)
			{
				/*Replacing the word with the space*/
				strcpy(word, " ");
				fputs(word, nf);
			}
			else
			{
				fputs(word, nf);
				fputc(' ', nf);
			}
			i = -1;
			/*After the loop ends, i increament by 1,
			so -1 becomes 0 and	next character saved
			in the word[0]*/
		}
	}
}

© 2020 Garbage Valuegarbage value logo