Reputation: 1632
TODO: If a certain word exists in the .txt file, copy that word to another txt file
PROBLEM: It won't write the word after it is found in "from.txt" to "to.txt".
ERROR:
This line: while ((fscanf(ifp, "%s", line)) != EOF)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_LINE 256
void main()
{
FILE *ifp;
FILE *ofp;
char line[MAX_LINE];
char word[MAX_LINE];
if ((ifp = open("from.txt", "r")) == NULL)
{
printf("Can't open input file.");
exit(1);
}
if ((ofp = open("to.txt", "w")) == NULL)
{
printf("Can't open output file.");
exit(1);
}
printf("Enter your word: ");
gets(word);
while ((fscanf(ifp, "%s", line)) != EOF)
{
if (strcmp(line, word) == 0)
{
fputs(line, ofp);
break;
}
}
fclose(ifp);
fclose(ofp);
getch();
}
Upvotes: 1
Views: 111
Reputation: 23218
PROBLEM: It won't write the word after it is found in "from.txt" to "to.txt".
As noted in comments and other answers, and for other reasons, open()
may not be the best choice for writing strictly ANSI portable code.
But this is not the reason for the stated problem.
The function strcmp(...)
is not doing what is needed.
In this line:
if (strcmp(line, word) == 0)
A single word is being compared with the entire line. And the single word is never identified. Even if the line in the file appears to have only a single word, white space, such as a space, tab or new line character ( " ". \n, \t) would cause the two arguments of strcmp
to be unequal.
strcmp(string1, string2) possible return values are:
Positive integer when string1 is greater than string2
Zero when string1 is equal to string2
Negative integer when string1 is less than string2
The function strstr
would be a better fit. Change the strcmp line to use strstr :
if (strstr(line, word)){...
strstr(...) looks for the existence of a sub-string within a string. And, with the other changes that have been discussed, made your code do as you described it should.
Upvotes: 1
Reputation: 132
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_LINE 256
void main()
{
FILE *ifp;
FILE *ofp;
char line[MAX_LINE];
char word[MAX_LINE];
//**************************************************************** it's fopen not open ***********************************************************
if ((ifp = fopen("from.txt", "r")) == NULL)
{
printf("Can't open input file.");
exit(1);
}
if ((ofp = fopen("to.txt", "w")) == NULL)
{
printf("Can't open output file.");
exit(1);
}
printf("Enter your word: ");
gets(word);
while ((fscanf(ifp, "%s", line)) != EOF)
{
if (strcmp(line, word) == 0)
{
fputs(line, ofp);
break;
}
}
fclose(ifp);
fclose(ofp);
getch();
}
its working fine ...
Upvotes: 1
Reputation: 1170
You are using a wrong API for opening a file. API which you use -- open
-- is for low-level, descriptor based access. You'll get an int
value out of it, and ifp
and ofp
won't be correct.
You must use a stream based API, called fopen
. It returns a pointer to the FILE
structure, which in turn you can pass to fscanf()
etc.
Very important: compile this program with all compiler warnings and observe the output. I'm pretty sure you're getting a log of warning messages from the compiler already.
Upvotes: 3