0

I am trying to remove particular words from a file and print the other words but the code is not doing what it is supposed to do I have debugged like crazy and the answers don't make sense I find it hard to understand why it's doing it like this? NB all the word in the original out1.data.txt file are on separate lines

Bellow is my code:

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

int
main(void)
{
    FILE *inp;
    FILE *outp;

    int i, found;
    char word[16];
    char toremove[][5] = {"is\n", "a\n", "and\n", "with\n", "By\n", "that\n", "to\n", "It\n", "has\n", "in\n", "for\n", "from\n"};

    inp = fopen("out1.dat.txt", "r");
    outp = fopen("out2.dat.txt", "w");

    while(!feof(inp))
    {
        for(i = 0; i < 13; i++)
        {
            printf("%s", toremove[i]);
            found = 0;
            fgets(word, 16, inp);

            if(strcmp(word, toremove[i]) == 0)
            {
                printf("%d", strcmp(word, toremove[i]));
                found = 1;
            }
            else
            {
               i = 14;  // had to set it to outside it parameters if found
            }

            if(found != 1)
            {
                printf("%s", word);
                printf("%d ", strcmp(word, toremove[i]));
            }
        }
    }

    fclose(inp);
    fclose(outp);

    return(0);
}
Paul
  • 1,630
  • 1
  • 16
  • 23
Tom Snow
  • 3
  • 3
  • Please be more specific about what are you going to do. – Paul May 30 '21 at 11:23
  • Please read [why while(!feof(f)) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Ed Heal May 30 '21 at 11:35

3 Answers3

0

There were a few issues in your code. I've made the fixes and added the comments with !.

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

int main(void)
{

  FILE *inp;
  FILE *outp;

  int i, found;
  char word[16];
  // !The length of the longest string is 6: [ with\n\0 ].
  // !You have to take null character into account 
  char toremove[][6] = {"is\n", "a\n", "and\n", "with\n", "By\n", "that\n", "to\n", "It\n", "has\n", "in\n", "for\n", "from\n"};

  inp = fopen("out1.dat.txt", "r");
  outp = fopen("out2.dat.txt", "w");

  while (!feof(inp))
  {

    fgets(word, 16, inp);
    int toremoveLen = sizeof(toremove) / sizeof(toremove[0]); // ! Length of the list
    for (i = 0; i < toremoveLen; i++)
    {
      // printf(toremove[i]);
      found = 0;

      if (strcmp(word, toremove[i]) == 0)
      {
        printf(strcmp(word, toremove[i]));
        found = 1;
        break; // !If the word if found, no need to compare it with others
      }
    }
    if (found != 1) // !Print the word only if it does not match with any word in the blacklist
    {
      printf("%s", word);
      // printf(strcmp(word, toremove[i]));
    }
  }

  fclose(inp);
  fclose(outp);

  return (0);
}
nascarsayan
  • 166
  • 3
  • 7
0

1)Open source file in r (read) mode. Store its reference in a FILE pointer variable say fPtr.
2)Create and open a temporary file say delete.tmp in w (write) mode. Store its reference in a variable say fTemp.
3)Read word to remove from user in some variable say toRemove.
4)Read a line from source file fPtr and store it in temporary buffer variable.
5)Remove all occurrence of given word from buffer and write buffer to temporary file fTemp.
6)Repeat step 4-5 till end of source file.
7)Close both file fPtr and fTemp.
8)Delete source file using remove() function.
9)Rename temporary file with source file name using rename() function. And we are done. FOR CODE LINK

Ayush Jha
  • 1
  • 1
0

I ended up figuring it out my self thank you though and i came up with a more simplified version as shown below:

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

int
main(void)
{

FILE *inp;
FILE *outp;

int i, j;
char word[16];
char toremove[][6] = {"is\n", "a\n", "and\n", "with\n", "By\n", "that\n", "to\n", 

"It\n", "has\n", "in\n", "for\n", "from\n"};

inp = fopen("out1.dat.txt", "r");
outp = fopen("out2.dat.txt", "w");

while(!feof(inp))
{

fgets(word, 16, inp);
for(i = 0; i < 12; i++)
{
    j = strcmp(word, toremove[i]);

    if(j == 0)
        i = 12;
}

if(j != 0)
    {
    fprintf(outp, "%s", word);
    }
}

fclose(inp);
fclose(outp);

return(0);
}
Tom Snow
  • 3
  • 3