0

I am very new to working with files and I can't seem to get my head around this. What I am trying to do is to write in the Exit.txt file all the lines that have my given word in them. For example, if my word is "exercise" and my In.txt contains the following:

I exercise daily
I like apples
How often do you exercise
I am tired

Then in Exit.txt I should have

I exercise daily
How often do you exercise

The problem is that somehow it only writes the last line in the Exit.txt file, and sometimes it doesn't even write anything, depending on my input In.txt.

I would very much appreciate any help, thank you very much!

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

    int main()
    {
      FILE* fis, * fis2;
      char* sir, * rez, word[50];
    printf("Word: ");
        gets(word);
    sir = malloc(50 * sizeof(char));
    fis = fopen("In.txt", "rt");
    if (fis == NULL)
        printf("Can't open file!");
    else
    {
        while (!feof(fis))
        {
            rez = fgets(sir, 50, fis);
                if (strcmp(rez,word)==0)
                {
                    fis2 = fopen("Exit.txt", "wt");
                    fputs(sir, fis2);
                }
        }
    }
    fclose(fis);
    free(sir);
    return 0;
 }
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55

2 Answers2

0

When you open a file for writing in a loop you must know about the offset. Its better open file in append mode, write your data and close it.

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

int main()
{
    FILE* fis, * fis2;
    char* sir, * rez, word[50];
    char *line = NULL;
    ssize_t bufsiz = 0;
    ssize_t nbytes;
  
    printf("Word: ");
    gets(word);
    fis = fopen("In.txt", "rt");
    if (fis == NULL)
        printf("Can't open file!");
    else
    {
        while ((nbytes = getline(&line, &bufsiz, fis)) != -1)
        {
            char * ptr_value = strstr(line,word);
            if(ptr_value != NULL) {
                printf(line);
                fis2 = fopen("Exit.txt", "a");
                fputs(line, fis2);
                fclose(fis2);               
            }       
        }       
    }
    fclose(fis);
    return 0;
 }
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
-1

there are a few changes needed to get your code working:

  • only open the output file once (so not in the while loop)
  • to check if a string is a part of another string please use "strstr" and not "strcmp"
  • don't forget to close your file at the end

So here is a suggestion for a solution

int main()
{
    FILE* fis, * fis2;
    char* sir, * rez, word[50];
    printf("Word: ");
    gets(word);
    sir = malloc(50 * sizeof(char));
    fis = fopen("In.txt", "rt");
    if (fis == NULL)
        printf("Can't open file!");
    if ((fis2 = fopen("Exit.txt","wt"))==NULL){
        printf("Cant't open Exit-file\n");
        return EXIT_FAILURE;
    }
    else
    {
        while (!feof(fis))
        {
            rez = fgets(sir, 50, fis);
            if(strstr(rez,word)!=NULL)
            {
                fputs(sir, fis2);
            }
        }
    }
fclose(fis);
fclose(fis2);
free(sir);
return 0;
}
Bert
  • 57
  • 6