Thread: How to remove all occurrences of a WORD from a FILE in C

  1. #16
    Registered User
    Join Date
    Oct 2018
    Posts
    30
    Kernelpanic is right! Something is wrong with the code itself and the modifications to use files must have brought out the bug. It was working as I stated. I complie and ran it at least 15 or more times just to make sure.

    Then I shut my computer down and when shopping for food. I get home; I turn on the computer, I ran the program and this is what I got just now:

    Code:
    removeremoveThisremoveremove   ---  1.txt
    
       
    
      4 occurrences of 'remove' removed successfully. --- a fib
    
       
      This      --- result 2.txt


    I think it is leaking memory or something. I think no one should test this code unless it is in a VM, already backed-up until we figure out what's going on. Before I went shopping it ran perfect. So it fooled the complier until a reboot or shutdown. I always anyway reboot or shutdown once I see that what I done is working. After a reboot or shutdown the truth come out. I seen that a few times during build of my C file. It told a fib. I fix it, and then reboot again until I know the complier got it right. Weard but true. Kernelpanic got it off the top. So GCC must work better on Windows or something.

    One thing for sure, I now know how to connect files to those file-less examples so I can try others or anything. So this is not a total lost at all. I'm going to study the code now. I might learn something.
    Last edited by fat32; 11-06-2018 at 06:20 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?

    Also, from what you posted, it looks like whatever you tried is working as expected. How does it not work?

    Concerning the code I posted: no, Kernelpanic is wrong. The code works, and Kernelpanic's own post shows that it does. The warning is because of what I told you earlier about how there's a potential bug, and hence I inserted a warning when the condition in which the bug might manifest happens (but somehow Kernelpanic either didn't read my earlier post, or didn't consider the bug). Unfortunately, it is difficult to distinguish between this condition and a situation where the input is a single line (or more generally, the last line) not terminated by a newline. This could be fixed, but then you might as well put in the effort to eliminate the bug entirely, which would require dynamic memory allocation or some smart checking of the end of the line that is beyond the scope of my example.
    Last edited by laserlight; 11-06-2018 at 06:46 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by laserlight View Post
    You should end your input text file with a new line, just like you would with C source code.
    OK, I have do it - all text-files new. Program new compiled, it is always still the same problem.
    So, one word will not show in the second file without warning, because the remove word will remove in the second file. So one see nothing. Is this correct?


    If yes, then ist there a logical mistake. If no, then I don't know what it is.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kernelpanic
    OK, I have do it - all text-files new. Program new compiled, it is always still the same problem.
    So, one word will not show in the second file without warning, because the remove word will remove in the second file. So one see nothing. Is this correct?
    I don't understand what you mean. Post the test input, expected output, and actual output. You must post the expected output so I know what you think the code should be doing, otherwise I might look at the actual output and go "what's the problem?" whereas you look at the actual output and go "it's obviously wrong" and so we end up speaking past each other.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Could it be a UTF-8 problem?
    EDIT: Actually, I guess it should work with multi-byte characters.
    Last edited by john.c; 11-06-2018 at 07:36 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by john.c
    Could it be a UTF-8 problem?
    I don't see any problem in the first place. If you look at Kernelpanic's post #14, Kernelpanic gives two test runs. The first involves test input consisting of a single word, i.e., the word to be removed, and the actual output is blank. That is exactly the expected output, yet Kernelpanic marked it with a question mark. Yes, there was the warning that shouldn't be there, but it can be ignored.

    In the second example... what on earth is wrong? It looks perfectly fine to me because the word was correctly removed.
    Last edited by laserlight; 11-06-2018 at 07:52 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    I see. I looked through the code and it looks good. Nice division of work so that it's pretty easy to see that it (looks!) correct. A couple of little tests work fine for me, too.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I realised that fixing the spurious warning for a line that doesn't end in a new line isn't that involved. We just need to introduce a bit of state:
    Code:
    size_t copyWithoutRemovedWord(FILE *output_fp,
                                  FILE *input_fp,
                                  const char *word_to_remove)
    {
        size_t remove_count = 0;
        int partial_read = 0;
        char buffer[BUFSIZ];
        while (fgets(buffer, BUFSIZ, input_fp))
        {
            /* There is a potential bug when a line is too long to fit in the
               buffer, so we shall warn the user if that condition occurs: */
            if (partial_read)
            {
                fprintf(stderr,
                        "Warning: the word to remove might not be found as the "
                        "line was only partially read for the searching.\n");
            }
            partial_read = !strchr(buffer, '\n');
     
            remove_count += removeAll(buffer, word_to_remove);
            fputs(buffer, output_fp);
        }
        return remove_count;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    That handles it neatly. Kind of a delayed reaction.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I thought I'd revisit that link that you posted, fat32...

    Quote Originally Posted by fat32 View Post
    Err... that's not the original author of the code I wrote. I am the original author of the code that I wrote; at best you might say that I drew some inspiration from the code you posted in post #1 in terms of some aspects of the algorithm and the function named removeAll, yet you didn't identify who originally wrote that code before you modified it. This C program that you linked to takes a very different approach: parse the file content into "words" (in the sense of whitespace delimited contiguous portion of non-whitespace text), and then join the words again, without those words that match the word to delete. This means that if the word to remove is part of a longer word, it won't be removed. It also means that your original whitespace will not be retained, but rather will be replaced by a space each. Whether this is desirable or not depends on your requirements; you didn't carefully specify your requirements. Also, note that this author is completely cavalier about buffer overflow: that code is riddled with potential buffer overflow problems. You also might want to note that the use of strcpy is technically wrong, even though it probably works: the arguments to strcpy must point to strings that do not overlap.
    Last edited by laserlight; 11-07-2018 at 02:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by laserlight
    In the second example... what on earth is wrong? It looks perfectly fine to me because the word was correctly removed.
    Nothing! It should show that the program running correctly. I should have written it, my mistake. In post #14 already I had the thought that I could have misunderstood something. "It should not appear in the output-file . . . then it also run correctly . . .".

    I was a bit confused by the compiler message but now I know that the program run correctely. If in the first-file there only one word for removing then logically is in the second-file (Output-file) nothing to see.

    Alrighty! The program run absolute correctely. Sorry for caused wrong alarm!

  12. #27
    Registered User
    Join Date
    Oct 2018
    Posts
    30
    Laserlight I was just pointing out where I found the example since I jack it all up. I wanted to edit that post yesterday but the edit feature was removed. Too long, I guess. Maybe the Administrator could change the statement to something more appropriate or remove it. Please do so Admin once you review this post. While here; you should allow members to edit their post as many time as he want for the first hour before marking it as edited. I can't spell or speak like a pro, so I edit to get as close as possble. Other than that this forum is truly perfect. Easy on the eye and everything. See ya!


    By the way; your code can even find and remove standalone symbols, or should I say anything I type on the keyboard. For characters only, even the so-called best of examples will fail to remove the letter (i) or (') oand certain combination of symbols, such as (//). That's the other advantage of your code. It does single character too or a string of them. All-In-One. UNBELIIEVABLE!

    Don’t take my word for it mates, just Google today before they all get hip.
    Last edited by fat32; 11-07-2018 at 12:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove uppercase case letters from a word
    By Sid_TheBeginner in forum C Programming
    Replies: 12
    Last Post: 10-25-2012, 09:05 PM
  2. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  3. Remove all occurrences of character from string
    By Scotty33 in forum C Programming
    Replies: 14
    Last Post: 09-27-2011, 04:32 AM
  4. Counting occurrences of a word in a string
    By mrodgers in forum C Programming
    Replies: 3
    Last Post: 03-14-2010, 11:31 AM

Tags for this Thread