NevemTeve gave a perfectly good answer back at post #33.
|
Only if the leading char is # and not in the middle of after the string (line).
|
Quote:
Quote:
Quote:
Quote:
Quote:
I've been trying to say, do NOT worry about a fully blown 1,000 line program, worry about this one task, get it done, get it done fast and efficiently. Please see NevemTeve's very perfect code for that. Quote:
This is repeated advice. Rather wish you would actually work at learning to debug and paying attention to the results and impact of each statement. This also is something which phil.d.g, along with others have tried to guide you towards. Because "giving" someone a full answer doesn't always work. I fully do not admonish NevemTeve's post of exactly that, I find it makes my point. The fact that you didn't heed it, recognize it, or apparently even try it, is a common and disappointing theme. You are learning, do not get discouraged, but do understand that I'm also not going to stand by and cheer you for posting. I'm trying to get you to turn on the switch of effort with paying attention to debugging and the impact of each code line you write and each change you make to it. I do not need an encyclopedia in response here, just saying. Instead, what I'd like is for you to use NevemTeve's example, and ONLY their code, along with the very minor support things you need, to validate that they've shown you the way, or at least one way, and that it is effective. Then please consider further questions to expand that topic. |
Quote:
|
Quote:
geany open and use find on it, or anything with the leading # is a comment else it is not, then be done with it. |
Quote:
|
Quote:
Quote:
Are you sure you don't know how to do that? We (as in just me and you) were examining code a few posts back that tried to do exactly that: Code:
if ( content[i] == '#' ) { |
Quote:
Quote:
Quote:
Example: take my original problem with the condition with my for loop; it was very obvious that the loop was terminating before reaching the hash if it was more than 32 spaces from the start of the line. But, it was NOT obvious WHY, gdb, as well as the printf()'s I tried before posting this thread did not give me any idea WHY the original for loop condition I had was wrong. It was only when astrogeek gave the hint about what it was comparing to (being ASCII), and if you read back through this thread, you'll see that while it sorta became clearer, it wasn't until astrogeek posted his printf() statement so I could SEE it's output leading up to the loop terminating combined with GazL's post #13, and some experimenting on my part that it finally clicked as to what astrogeek was saying in post #2. You seem to think that it would have just "jumped out at me" if I run gdb enough times or some such. Well, I wish you were right about that RT, but unfortunately, that's not the case. Why did astrogeek's and GazL's post mentioned above really help? Because both of them and their posts gave me something to go on (and same for phil.d.g's posts later on for that matter), then I could actually start understanding what my for loop condition was actually doing (and I could then SEE what was really going on with it), and moreover WHY it was wrong. In other words: not just, "just trust me, it's wrong", or "this is a better way of doing it"... In any case, while my learning style may well seem ineffective to your RT, and much as it's not your learning style; I have to do what works for me, and the example I just gave above DID work for me. Perhaps you can just step through it (and likely can, unlike me obviously), debug it with gdb or whatever, and figure out what the actual problem is, not to mention you'd also have probably 20+ year worth of experience doing just that. I don't have 20+ years of experience like you (and probably like most of the others responding to this thread), my learning style is obviously very different to yours, OK, fine. You do what works for you, I can only do what works for me. So just using someone else's code doesn't tell me WHY it's a better way of doing it, let alone why what I've tried to write doesn't work the way I intended it to. It's like saying "just trust me, it's better", well, I may as well just give up and not even bother trying to learn C in that case. Sorry for my "encyclopedia" response, but (and sorry for the "but" too) I felt is was warranted to explain what I did above. But I won't beat that drum anymore though. Quote:
Quote:
|
Compiling the original program from post #1 with -ggdb and using the debugger.
Setting a breakpoint at the "for ( i = 0; i < content[i]; i++ )" line. r (hit breakpoint) p content content = "line 1" c (hit breakpoint) c (hit breakpoint) c (hit breakpoint) c (hit breakpoint) c (hit breakpoint) p content content = " # comment 3" c (hit breakpoint) p content content = ' ' <repeats 33 times>, "#comment 4" (There's a bunch of garbage and other stuff after the \n for each line, you get to see it because the content array is 373 characters long, no biggie) s (single step between the for statement and the if(content == '#') statement) p i p content p content[i] And so forth will output the values of i, content, and content located at i Single step until it eventually breaks out of the loop and goes to the if(str) line. The program flow has made a conclusion and did not choose the path saying "found #" Therefore what happened here? Why did it not get to the #? What is the value of i? p i i is 32 p content[i] content[32] is 32 ' ' Since 32 == 32, it exited the for loop. Yes it is tedious, but it is clear what every variable value is, what is in the content[] array, and what the value of the loop index i, is. At each and every single step you can print out i, content[i], and predict what the outcome of the next line of code is. You also can single step as it proceeds through the for() and if() statements and the first moment it either goes to the 'str = false' line or the if(str) line, there has been a qualifying change to cause it to exit that loop. Right at that point, examine (print) the variable values and try to figure out what is going on. That's how you debug. whatever you're doing, ain't that. |
Thank you RT, while that is helpful; I did setup a breakpoint just before the for loop, and stepped through each statement after it when I ran gdb on the "original version" of my program. But, I didn't make the connection between the fact it was comparing to ASCII, and therefore once it hit the space, that was causing the loop to stop before it got to the hash if it was more than 32 spaces from the start of the line. So yes, I did see what you're saying above about the space, and yes, I did print out the value of the array element it was up to, but like I said, I still wasn't making the connection - that's what I meant about not seeing anything that gave me any idea as to what astrogeek was talking about in post #2. But yes, I do get what you're saying now though. Thanks for your help.
Anyhow, and sorry phil.d.g, I think I may have gotten confused about what you were saying before, as I re-read your posts, and hopefully I get what you mean now. Although, I'm not sure if you meant "print the first character after any spaces for all of the lines in the file", or "print the first character after any spaces for just one line". So the code below will print out the first character after any spaces for all of the lines in the file. I tried again to try and get it to ignore anything with a hash before any characters bar any spaces, but not ignore the line if the hash isn't in front of any characters bar the space, and print out the whole line including what's after the hash, but on the same line, but I still couldn't figure it out. I managed to to everything but that though. Anyway, here's the code as it stands now; Code:
#define CONTENT_LEN 373 Code:
No comment Code:
james@jamespc: practice> ./skip_line_if_comment |
Quote:
So, what you've done is search for and print out the single character that is needed to make the decision "Is this line a comment or not", and then, for that line, stop looking at characters since we already have enough information. I'm glad you've also attempted to move onto the next stage: Quote:
Your code and comments indicate that you do not fully understand what the break and continue keywords do. Can I suggest reading the following: https://www.tutorialspoint.com/cprog..._statement.htm https://www.tutorialspoint.com/cprog..._statement.htm And then have another attempt at figuring out where the commented out if statement I said we're going to need belongs. |
Thanks, I thought that's what you probably asking, but I still wasn't 100% sure I was thinking right.
I think you're right about the break and continue statements - I'm not sure the continue statement has ever been completely clear to me, beyond the obvious/the normal meaning of the word, as in: "keep doing it". I read the links you posted, but I think from memory that's the site I first started trying to follow when I first started trying to learn C, and then giving up on that same site because it just wasn't detailed enough, and the way it explains stuff was just too confusing and too brief for me to be able to properly understand the various concepts it talks about. I did find this site explaining the continue statement which seems at least a little clearer and easier to follow. So to try and clear up any misunderstandings; does the break statement basically mean "stop and exit the loop or switch statement"? Does the continue statement (based on the link I posted above) basically mean instead of executing the following statements in the loop body, re-evaluate the loop condition? Beyond that, it really isn't clear to me. Anyway, and back to the for loop; Based on what I think I understand about the break and continue statements, I cannot see how any of them fit into the if statement that runs if content[i] is not equal to a space. If I do need to use one of them, I'm honestly buggered if I know which one. I tried figuring out where I supposed to put the if statement you quoted above, and while I **think** it should be nested within the if statement that checks if content[i] is not equal to a space, I'm not 100% sure about that. Beyond that, I'm still pretty unclear on how to do what I was talking about before, and this is where I kept getting stuck before. |
Quote:
Some folk actually hold the view that it's bad style to use break/continue at all. I don't agree as I think they have their place and are fine as long as you don't misuse them, but often you simply don't need break/continue. |
break = kicks it out of the loop. (switch too if using one).
continue = kicks it back to the condition, to loop again. |
Quote:
Quote:
Quote:
Quote:
GazL has a point with respect to style. However, let's not worry about that for now. I'm trying to lead you to a solution using constructs and techniques you've previously demonstrated using. It's your solution, not mine. Worrying about style, efficiency, and even 100% correctness is a job for another day. |
All times are GMT -5. The time now is 03:50 AM. |