Thread: File problem

  1. #16
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    That's good, but that also means we have no way of verifying what you did to try and remove the newlines.
    <skpped>
    The core if your response was a supposed hidden new line. That was clanger. You have it wrong. The cause was the algorithm.
    Last edited by zach; 09-29-2019 at 12:45 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by zach
    The core if your response was a supposed hidden new line. That was clanger. You have it wrong. The cause was the algorithm.
    The bug was due to a newline that you didn't account for. It wasn't due to your algorithm: if you correctly removed the newline while retaining the overall original algorithm, the bug would have been fixed. That you fixed the problem by implementing a different algorithm doesn't mean that flp1969 and I were wrong about our analysis concerning your bug as posted in post #1.
    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
    zach
    Guest
    You seem to be oblivious to the error in the algorithm. Did you have a good look at it? You simply have it wrong. And might we please limit the discussion to the issue per se?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Yes, I had a good look at your implementation of your algorithm as posted in post #1. How do I have it wrong?

    Notice that you never explained what exactly it is that you're trying to do. You only posted code and outlined a bug. So, if there's an error with your algorithm itself beyond the bug that you mentioned, no one else can find it and fix it, i.e., there is no way I can be wrong about your algorithm because it is yours, not mine. I can be wrong about my analysis concerning the bug that you mentioned.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    It might be easier if we looked at code. Here's a test data5.txt:
    Code:
    This is
    a
    test
    for
    new lines
    Here's your code from post #1 placed within a self-contained program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *fp = fopen("data5.txt", "r");
        if (fp == NULL)
        {
            puts("No data5.txt with codes.");
            return EXIT_FAILURE;
        }
    
        int lineNo[] = {0, 1, 0, 1, 0, 1, 0};
    
        int counter = 0;
        char str[35];
        while (counter + 1 < sizeof(lineNo) / sizeof(lineNo[0]) && fgets(str, sizeof(str), fp))
        {
            if (lineNo[counter + 1] == 1)
            {
                puts(str); // ***
            }
            counter++;
        }
        fclose (fp);
    
        return 0;
    }
    I've taken the liberty of adding the counter + 1 < sizeof(lineNo) / sizeof(lineNo[0]) check in order to avoid array out of bounds access, and changed the fgets call so that it is not dependent on a magic number. I also removed the gotoxy calls since they are not needed for this demonstration, and after all are non-standard so it is more tricky to come up with a portable example program.

    Here's the output from compiling and running the above program with my data5.txt:
    Code:
    This is
    
    test
    
    new lines
    We can see that when lineNo[counter + 1] == 1, the corresponding line from data5.txt is printed, but it looks like when lineNo[counter + 1] == 0, the corresponding line from data5.txt is not printed, yet a blank line (i.e., a newline on its own) is printed. This matches your statement from post #1:
    Quote Originally Posted by zach View Post
    This *** printing should take place when lineNo[counter + 1] == 1 which it does correctly but when lineNo[counter + 1] == 0 a new line is being printed and that is unintended. Please help. Zach K.
    Now, let's change the code to remove the newline from fgets:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        FILE *fp = fopen("data5.txt", "r");
        if (fp == NULL)
        {
            puts("No data5.txt with codes.");
            return EXIT_FAILURE;
        }
    
        int lineNo[] = {0, 1, 0, 1, 0, 1, 0};
    
        int counter = 0;
        char str[35];
        while (counter + 1 < sizeof(lineNo) / sizeof(lineNo[0]) && fgets(str, sizeof(str), fp))
        {
            if (lineNo[counter + 1] == 1)
            {
                str[strcspn(str, "\n")] = '\0';
                puts(str); // ***
            }
            counter++;
        }
        fclose (fp);
    
        return 0;
    }
    Now, when we run the program with the given data5.txt, we get:
    Code:
    This is
    test
    new lines
    This demonstrates that indeed the bug has to do with the combination of the newline being left in the buffer from fgets, and puts writing its own newline.
    Last edited by laserlight; 09-29-2019 at 02:42 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

  6. #21
    zach
    Guest
    "This demonstrates that indeed the bug has to do with the combination of the newline being left in the buffer from fgets, and puts writing its own newline."

    No laserlight quasi new lines are printed because of counter ++ ( line 25) causes the cursor to drop when lineNo = 0. You shouldn't change the code in an attempt to justify your point, that is very dirty. You simply didn't have the insight to see how the algorithm was producing new lines. Covering up that you missed the point by changing the code is despicable.
    Last edited by zach; 10-01-2019 at 01:52 AM.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by zach
    No laserlight quasi new lines are printed because of counter ++ ( line 25) causes the cursor to drop when lineNo = 0. You shouldn't change the code in an attempt to justify your point, that is very dirty. You simply didn't have the insight to see how the algorithm was producing new lines. Covering up that you missed the point by changing the code is despicable.
    Insulting a member for genuinely providing analysis is despicable. I'm not covering up anything. You can see for yourself that my code example does indeed produce the output you described. Removing the gotoxy call makes no difference because you don't have a gotoxy call when lineNo[counter + 1] == 0, and the gotoxy call when lineNo[counter + 1] == 1 happens before the puts call.
    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

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    If you do want to insist that I'm wrong, then do the same thing: come up with the smallest and simplest compilable program that demonstrates the problem you described in post #1. Provide the test input, expected output, and actual output.

    In fact, this is one of the best ways to proceed when asking for help: people could compile and run your program for themselves and maybe tweak it to see if their suggested fix worked. There's no point insulting people for a supposed "lack of insight" when you didn't provide a full picture for insight in the first place.
    Last edited by laserlight; 10-01-2019 at 03:29 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

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Actually, you can just modify my code from post #23, and you may want to change the test input too. I'm not going to do that for you because I'm not keen to venture a guess as to how you're working with the non-standard constructs, but after re-examining your point about gotoxy changing the behaviour, I can see what you mean since counter is incremented on every iteration, yet used in the gotoxy call.
    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

  10. #25
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    If you do want to insist that I'm wrong, then do the same thing: come up with the smallest and simplest compilable program that demonstrates the problem you described in post #1. Provide the test input, expected output, and actual output.

    In fact, this is one of the best ways to proceed when asking for help: people could compile and run your program for themselves and maybe tweak it to see if their suggested fix worked. There's no point insulting people for a supposed "lack of insight" when you didn't provide a full picture for insight in the first place.
    You simply don't have the wisdom to follow the while wend loop for a few cycles in order to see what the outcome is on the screen. That is, without butchering my code about to support your erroneous position. Which, by the way, is enough to make anyone angry. You need someone to take you by the hand and explain to you how the algorithm inserts empty lines. "you didn't provide a full picture for insight in the first place": I cannot explain to a fish that it is swimming in water, you are missing a few IQs my dear. I am not going to continue this discussion ad infinitum.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by zach
    You simply don't have the wisdom to follow the while wend loop for a few cycles in order to see what the outcome is on the screen.
    I use Linux, so I cannot see what the output is on screen because I cannot compile a non-standard program that uses gotoxy. That's another reason why providing a test program with test input and output is important.

    Quote Originally Posted by zach
    You need someone to take you by the hand and explain to you how the algorithm inserts empty lines. "you didn't provide a full picture for insight in the first place": I cannot explain to a fish that it is swimming in water, you are missing a few IQs my dear.
    You can stop insulting me now. The fact is that I explained my position, you merely asserted without proof that that was not the problem, but rather it was with your algorithm. Asking for an explanation is a far cry from "hand holding".

    As I implied in my previous post, back in post #16, instead of just claiming without proof that "the cause was the algorithm", you could have pointed out that because counter is incremented on every iteration and used in the gotoxy call, it affects the positioning by gotoxy such that lines appear to be skipped.

    It's that simple, but because you neither provided a small program with test input and output nor included code to remove the newline in your code, flp1969 identified another potential problem that turned out to be tangential, and that reasoning made sense to me because there was no evidence to the contrary.
    Last edited by laserlight; 10-01-2019 at 04:39 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

  12. #27
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    I use Linux, so I cannot see what the output is on screen because I cannot compile a non-standard program that uses gotoxy. That's another reason why providing a test program with test input and output is important. <snipped>
    You have drawn a line under the discussion, for which I thank you. I will do the same: I did explicitly describe the nature of the bug to you in very clear terms. Therefore the simplicity of the bug on the one hand, and you doggedly ignoring my explanation on the other hand, gave rise to my impression that you were not being sincere with me. That in turn caused my expressed disrespect of your vast experience and knowledge with which you make so many valuable contributions to discussions in this forum. That I regret. With these words I would please also like to close the discussion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handing and saving records to file problem
    By loveable in forum C Programming
    Replies: 18
    Last Post: 12-29-2012, 09:53 AM
  2. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 10:43 AM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. Replies: 3
    Last Post: 10-20-2006, 07:59 PM
  5. Replies: 3
    Last Post: 05-03-2003, 12:04 PM

Tags for this Thread