1

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();
}
Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44

3 Answers3

3

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.

  • Well, now it says I can't open the file. – Ilan Aizelman WS Mar 18 '16 at 19:02
  • You didn't post much about where/how you compile it. With ``fopen("file.txt", "r")`` it expects you have ``file.txt`` in the very same directory from which you run the program. Otherwise you must specify literal path, e.g.: ``/path/to/file.txt``. – Wojciech Adam Koszek Mar 18 '16 at 19:07
  • Wrote a program to write into "to123.txt" file. it created the file in the directory, and I copied there the 2 files, and now the program works. the solution also was to find the correct directory. Problem solved. – Ilan Aizelman WS Mar 18 '16 at 19:09
  • 1
    To create the output file if it doesn't exist or truncate it and overwrite it if it does exist, for the ``fopen`` on ``ofp`` use ``w+`` mode specifier. You can also make this program better by accepting ``argv`` and passing the paths to the files from the command line. – Wojciech Adam Koszek Mar 18 '16 at 19:11
1
#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 ...

Avinash Nath
  • 132
  • 8
1

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.

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87