Thread: File problem

  1. #1
    zach
    Guest

    File problem

    Code:
    FILE *fp;
        char *p;
        char str[35];
        int counter = 0;
        
        fp = fopen("data5.txt","r");
        if(fp == NULL)
        {
            gotoxy(70,5);
            puts("No data5.txt with codes.");
        }    
    
        while(fgets(str,35,fp)!= NULL)
        {
            if(lineNo[counter + 1] == 1)
            {
                gotoxy(30,counter + 4);
                puts(str);//  ***
            }
            counter++;
        } 
        fclose (fp);


    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.
    Last edited by zach; 09-28-2019 at 06:26 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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.
    It is printed because you tell the compiler to print a 2 newlines each time lineNo[counter+1] == 1.

    See: man fgets and man puts

  3. #3
    zach
    Guest
    Quote Originally Posted by flp1969 View Post
    It is printed because you tell the compiler to print a 2 newlines each time lineNo[counter+1] == 1.

    See: man fgets and man puts
    If that were the case there would be a line skipped after each intended line, which there isn't. So would you please explain your response to my query?

  4. #4
    zach
    Guest
    Quote Originally Posted by zach View Post
    If that were the case there would be a line skipped after each intended line, which there isn't. So would you please explain your response to my query?

    Thank goodness that in programming there generally are more than one way to code something. So I scrapped the function and took a different route. Hence I am retracting my query about the bug I couldn't find.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read the Fine Manual and you would learn why it did what it did!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    zach
    Guest
    "Read the Fine Manual and you would learn why it did what it did! " What a silly thing to say on a forum, or are you saying you don't know?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aiyo, just read the manual. For example:
    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.
    puts() writes the string s and a trailing newline to stdout.
    So if you're callings puts with a string that ends in a newline, then you'll end up printing two newlines for each puts call, exactly as flp1969 described. Your postulation that "if that were the case there would be a line skipped after each intended line" is incorrect: you're just misinterpreting the output when you claim that "when lineNo[counter + 1] == 0 a new line is being printed".
    Last edited by laserlight; 09-28-2019 at 05:20 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

  8. #8
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    Aiyo, just read the manual. For example:


    So if you're callings puts with a string that ends in a newline, then you'll end up printing two newlines for each puts call, exactly as flp1969 described. Your postulation that "if that were the case there would be a line skipped after each intended line" is incorrect: you're just misinterpreting the output when you claim that "when lineNo[counter + 1] == 0 a new line is being printed".
    Hi. I did, in fact, check extensively for new line characters at the end of the string in question! Now, in retrospect, I tend to think that at the heart of the problem, trailing white spaces were the culprits, causing removing new line characters at string-end to be ineffective. In the mean time I have chosen a different route: as it happens, a better one. (In this context I remember that in another language there used to be a right-trim and a left-trim (and also padding), that I used to use a lot. Yes, easily written.) By the way my program is littered with clean-ups. The dangle issue in C seems a bigger problem than C itself.

  9. #9
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    Aiyo, just read the manual. For example:


    So if you're callings puts with a string that ends in a newline, then you'll end up printing two newlines for each puts call, exactly as flp1969 described. Your postulation that "if that were the case there would be a line skipped after each intended line" is incorrect: you're just misinterpreting the output when you claim that "when lineNo[counter + 1] == 0 a new line is being printed".
    To which book are you referring with "the manual"?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zach
    I did, in fact, check extensively for new line characters at the end of the string in question!
    The code that you posted does no such thing though.

    Quote Originally Posted by zach
    Now, in retrospect, I tend to think that at the heart of the problem, trailing white spaces were the culprits, causing removing new line characters at string-end to be ineffective.
    I can think of two good ways of removing the trailing newline from fgets (one involving strchr and the other strcspn), and neither of them have the problem that you describe because they essentially involve searching for the newline in order to replace it with a null character.

    Quote Originally Posted by zach
    The dangle issue in C seems a bigger problem than C itself.
    There is a huge "dangle problem" in C involving buffer overflow vulnerabilities from string null termination or lack thereof, but this newline from fgets isn't it. You're just unfamiliar with the tool and blaming the tool instead of your lack of knowledge.

    Quote Originally Posted by zach
    In the mean time I have chosen a different route: as it happens, a better one.
    What route did you choose and how do you know it is better?

    Quote Originally Posted by zach
    To which book are you referring with "the manual"?
    This refers to the manual on *nix systems, available through the man command at the command line. These days you can find such manual pages online, typically by searching the Web with the same man <entry> command.

    Alternatively, you can refer to the C standard, or a late draft thereof. Or you can refer to various C reference pages available online. The point is to read documentation.
    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. #11
    zach
    Guest
    Laserlight, hi:

    1. I am not showing measures to get rid of "new lines" because I removed the various shots I took at the problem, in order to show the code that was bugged.
    2. "Lack of knowledge": I would have thought that was what this forum is all about.
    3. How I know the new route is better? The old route used a global array with 0-s and 1-s, guiding which descriptions to show / not to show. I was told, not to use globals - bad style. Therefore I consider the new routine a better one.
    2. "the man command at the command line" - I must again demonstrate my ignorance - dunno what that is.
    3. "These days" this seems to be a reference to my age.

    I am surprised given the skewness of the points you raise.
    Last edited by zach; 09-29-2019 at 05:05 AM.

  12. #12
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    The code that you posted does no such thing though.


    I can think of two good ways of removing the trailing newline from fgets
    Laserlight, Hi, I suggest you take another look at the code. You might see how the line skips came about. Zach K.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    The man command, is the command line or online manual for the C Programming Language, Linux commands, and other topics. It will describe the various functions, operators, and other information.

    You may not be on a UNIX or Linux system, so the online version is the one for you to use.

    For example, if I wanted information about the fgets() function, in Google, I would enter "man fgets" and hit search. Google would come back with several sites, but the one I recommend is the man7.org site.

    Click on the two links in this reply.

  14. #14
    zach
    Guest
    Quote Originally Posted by rstanley View Post
    The man command, is the command line or online manual for the C Programming Language, Linux commands, and other topics. It will describe the various functions, operators, and other information.< sbipped > Click on the two links in this reply.
    Thank you for this, I will have a good look a it. Zach K.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zach
    1. I am not showing measures to get rid of "new lines" because I removed the various shots I took at the problem, in order to show the code that was bugged.
    That's good, but that also means we have no way of verifying what you did to try and remove the newlines.

    Quote Originally Posted by zach
    2. "Lack of knowledge": I would have thought that was what this forum is all about.
    That's targeted specifically to your comment that "The dangle issue in C seems a bigger problem than C itself." There's no point pre-judging a tool until you know how to use it.

    Quote Originally Posted by zach
    3. How I know the new route is better? The old route used a global array with 0-s and 1-s, guiding which descriptions to show / not to show. I was told, not to use globals - bad style. Therefore I consider the new routine a better one.
    Ah, I see. What I was curious about is how you got round this fgets+puts issue without actually solving the bug. The issue sounds orthogonal to the issue of a global array (i.e., what you described does sound like an improvement, but in a different though related area).

    Quote Originally Posted by zach
    3. "These days" this seems to be a reference to my age.
    It's a reference to the fact that man pages are older than the Web. I don't even know if you're older than Unix or younger than the Web (though since you've referred to previous programming experience I guess you're older than the iPhone )

    Quote Originally Posted by zach
    I am surprised given the skewness of the points you raise.
    What do you mean?

    Quote Originally Posted by zach
    Laserlight, Hi, I suggest you take another look at the code. You might see how the line skips came about. Zach K.
    I thought you said that there were no (unintended) line skips:
    Quote Originally Posted by zach
    If that were the case there would be a line skipped after each intended line, which there isn't.
    Last edited by laserlight; 09-29-2019 at 11:26 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

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