LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2019, 06:54 AM   #61
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659

Thank you GazL, your insights are as always very helpful. Thanks again!

Thanks BW, glad I was somewhere near the mark

Quote:
Originally Posted by phil.d.g View Post
...
Have you tried this? Please do, and consider where it should belong relative to the break statement.
Yes I have tried this, I've posted the code of where I'm up to below.

Quote:
  1. What happens if you remove the break statement (no other changes) and run the program? Can you describe what has happened to the output? How is that different to the output generated when the break statement is present?
  2. From the output you pasted in your last post, where you printed the first non space character of each line, is that enough information to determine whether or not each individual line is a comment or not?
With the break statement present, it just prints the first character following a space. Without the break statement, it prints the whole string for each line after the space.

Yes, because if there is no hash before the string (not counting spaces when I say "string"), then it is not a comment. But this is the part that's baffling me; as it still sees the hash when it's after any other character, and therefore still calls those particular lines comments, when in that case it should not regard those lines as comments (including what's after the hash that's still on the same line, like for example "no# comment" - none of that string including the hash should be regarded as a comment) - this is the problem I'm trying to to solve without any luck.

Quote:
...The hardest part of programming, for me, is breaking down a problem into small, implementable steps, and then stringing those together to solve the big picture.
...
Yes, this is what's difficult for me too (as you may have noticed already).

Anyways, here's my code;

Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
                for ( i = 0; content[i] != '\0'; i++ ) {                
             //          printf("%i < %i (%c) ? %s\n", i, content[i], content[i], i<content[i] ? "True" : "False");                                                               
                    if ( content[i] != ' ' ) {
                       printf("content[i] = %c\n", content[i]);
                       if ( content[i] == '#' ) {                               
                          printf("content[i] = %c\n", content[i]);
                          break;
                       }
                    }              
                }
                    
                
    }
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
Thanks again for your help phil.d.g!
 
Old 10-20-2019, 04:21 PM   #62
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,271

Rep: Reputation: 152Reputation: 152
Quote:
Originally Posted by jsbjsb001 View Post
With the break statement present, it just prints the first character following a space. Without the break statement, it prints the whole string for each line after the space.
Not quite. Without the break statment it prints the line without any spaces. Accurate observation of behaviour and change in behaviour is a skill you need to develop.

Quote:
Originally Posted by jsbjsb001 View Post
Yes, because if there is no hash before the string (not counting spaces when I say "string"), then it is not a comment.
Cool. All we need to do now is implement making a decision on whether or not that character means the line is a comment or not, and then print the line.

Quote:
Originally Posted by jsbjsb001 View Post
But this is the part that's baffling me; as it still sees the hash when it's after any other character, and therefore still calls those particular lines comments, when in that case it should not regard those lines as comments (including what's after the hash that's still on the same line, like for example "no# comment" - none of that string including the hash should be regarded as a comment) - this is the problem I'm trying to to solve without any luck.
You have a small bug. Let's consider your code:


Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
                for ( i = 0; content[i] != '\0'; i++ ) {
                    // break; // (1)                                                                            
                    if ( content[i] != ' ' ) {
                       printf("Examining character '%c'\n", content[i]);
                       // break; // (2)   
                       if ( content[i] == '#' ) {                               
                          printf("This line is a comment because it starts with a '%c'\n", content[i]);
                          break; // (3)
                       }
                       // break; // (4)
                    }
                    // break; // (5)               
                }
                    
                
    }
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
I have changed what is printed out, such that, hopefully, you can better correlate the output with the code.

The above is your code. Unchanged, aside from comments. What happens if you comment out the break statment labelled 3 and in turn try uncommenting each other break statement? Before you do that, can you work out which break statement is likely to fix your bug?

Edit to add:

Since the start of this little journey post #40 we have solved a couple of bugs, and hopefully got you to start thinking about how to break down problems. It isn't as though I've sent you around the houses, just to end back at your original issue.

Last edited by phil.d.g; 10-20-2019 at 10:04 PM.
 
2 members found this post helpful.
Old 10-21-2019, 10:12 AM   #63
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
Quote:
Originally Posted by phil.d.g View Post
...
You have a small bug. Let's consider your code:
[code snipped]
The only difference I could see between having break statements and not having any at all was that, it still printed out the entire line (for all of them) if I didn't have any break statements. If that isn't the bug you're talking about, I'm honestly not sure what it is.

Quote:
The above is your code. Unchanged, aside from comments. What happens if you comment out the break statment labelled 3 and in turn try uncommenting each other break statement?
The for loop doesn't run at all because of the first break statement (break labeled 1), so it's not even going to get to any other break statements - let alone any of the if statements.

Quote:
Before you do that, can you work out which break statement is likely to fix your bug?
...
The only break statement I can see there that makes any sense is the break statement labeled 3.

But the program still should not see a line like "no# comment" as a comment, even though there clearly is a hash on that same line. Because the hash isn't before any other characters bar the space. So this is exactly where I'm baffled as to how to solve that, and which was exactly the problem I've been trying to solve without any success.

I tried to think what would happen with only one of the break statements uncommented from the first break (labeled 1) through to the last break before I tried anything. But I couldn't figure out what would happen if only the last break statement (labeled 5) was uncommented, so I had to compile it with just that break uncommented so I could see (I get it now though). So that's why I didn't put any comment after that break in my code below. I only wrote those comments before I tried anything, and have not added or changed any of them since (other than commenting in or out the break statements themselves to see if I was thinking right or not). Thanks again for your help.

Here's my code with some comments following all but the last break statement;

Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
          for ( i = 0; content[i] != '\0'; i++ ) {
    //        printf("%i < %i (%c) ? %s\n", i, content[i], content[i], i<content[i] ? "True" : "False");   
              // break; // (1)  for loop stops the for loop before it starts                                                                         
              if ( content[i] != ' ' ) {
                 printf("Examining character '%c'\n", content[i]);
                 //  break; // (2)   for loop stops after first char found that's not equal to a space
                 if ( content[i] == '#' ) {                               
                    printf("This line is a comment because it starts with a '%c'\n", content[i]);
                    break; // (3) for loop stops after it is not equal to a space but finds a hash. If the if statement is not equal to a hash, it displays each char following a space.
                 }
                 //      break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
              }
              //   break; // (5)               
                  
          }               
    }
    
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
 
Old 10-21-2019, 03:59 PM   #64
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,271

Rep: Reputation: 152Reputation: 152
Quote:
Originally Posted by jsbjsb001 View Post
The only difference I could see between having break statements and not having any at all was that, it still printed out the entire line (for all of them) if I didn't have any break statements. If that isn't the bug you're talking about, I'm honestly not sure what it is.
Let's park this and come back to it later. I'll ask you to compare what you had in post#40 with whatever you end up with, when we have something that works.

Quote:
Originally Posted by jsbjsb001 View Post
The only break statement I can see there that makes any sense is the break statement labeled 3.
Can you explain why that makes sense to you?

Code:
break; // (3) for loop stops after it is not equal to a space but finds a hash. If the if statement is not equal to a hash, it displays each char following a space.
Yes. Finds a hash, and doesn't stop until it does (or reaches the end of the line). It doesn't matter what characters preceed the hash. This is precisely what you don't want, it is the reason that 'no#comment' is considered a comment. There is a hash to be found.

Code:
break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
OK. Why is this something that you wouldn't want to do?

Have you compared the output of your program with only the third break statement uncommented and only the fourth break statement uncommented? Paste the two outputs here please.

For some light at the end of the tunnel: once you've sorted where the break statement belongs, there are only two changes (not additions) required to make the code do what you want.

Last edited by phil.d.g; 10-21-2019 at 04:02 PM.
 
2 members found this post helpful.
Old 10-22-2019, 02:00 AM   #65
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
Quote:
Originally Posted by phil.d.g View Post
...
Can you explain why that makes sense to you?
Because it doesn't stop until it printed the whole line - I was thinking that more code needed to be added to say "if anything other than a hash, even if there is a hash after other characters, display the line anyway". But yes, I see what you're saying now, so on second thought, it doesn't actually make sense (as you say). I guess that's what I wasn't getting before, in that, and like you said, it doesn't stop until it reaches the hash or the end of the line - keywords "doesn't stop".

Quote:
...
Code:
break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
OK. Why is this something that you wouldn't want to do?
On second thought, and given the above, then I can now see that break might actually make sense. But while I can sorta see what you're saying, I'm still not 100% sure about that particular break either. Other than to say based on the outputs from both having just the break labeled 3, and then just having the break labeled 4, I can see what you're saying. But still not really clear on the exact solution to the problem we are talking about (being that "no#comment" shouldn't be considered as a comment).

Quote:
Have you compared the output of your program with only the third break statement uncommented and only the fourth break statement uncommented? Paste the two outputs here please.
Yes, I've posted them below for you.

Quote:
For some light at the end of the tunnel: once you've sorted where the break statement belongs, there are only two changes (not additions) required to make the code do what you want.
I'll have to take your word for it at this point. Thanks again for the help!

Here's the outputs for you:

With just break labeled 3 uncommented;

Code:
james@jamespc: practice> ./skip_line_if_comment
Examining character 'N'
Examining character 'o'
Examining character 'c'
Examining character 'o'
Examining character 'm'
Examining character 'm'
Examining character 'e'
Examining character 'n'
Examining character 't'
Examining character '
'
Examining character '#'
This line is a comment because it starts with a '#'
Examining character 'N'
Examining character 'o'
Examining character 'c'
Examining character 'o'
Examining character 'm'
Examining character 'm'
Examining character 'e'
Examining character 'n'
Examining character 't'
Examining character '
'
Examining character 'n'
Examining character 'o'
Examining character '#'
This line is a comment because it starts with a '#'
Examining character '#'
This line is a comment because it starts with a '#'
With just break labeled 4 uncommented;

Code:
james@jamespc: practice> ./skip_line_if_comment
Examining character 'N'
Examining character '#'
This line is a comment because it starts with a '#'
Examining character 'N'
Examining character 'n'
Examining character '#'
This line is a comment because it starts with a '#'
 
Old 10-22-2019, 03:08 AM   #66
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,271

Rep: Reputation: 152Reputation: 152
Quote:
Originally Posted by jsbjsb001 View Post
Because it doesn't stop until it printed the whole line - I was thinking that more code needed to be added to say "if anything other than a hash, even if there is a hash after other characters, display the line anyway". But yes, I see what you're saying now, so on second thought, it doesn't actually make sense (as you say). I guess that's what I wasn't getting before, in that, and like you said, it doesn't stop until it reaches the hash or the end of the line - keywords "doesn't stop".

On second thought, and given the above, then I can now see that break might actually make sense. But while I can sorta see what you're saying, I'm still not 100% sure about that particular break either. Other than to say based on the outputs from both having just the break labeled 3, and then just having the break labeled 4, I can see what you're saying. But still not really clear on the exact solution to the problem we are talking about (being that "no#comment" shouldn't be considered as a comment).
Let's forget about printing lines for a minute. Does the version with break #4 consider 'no#comment' a comment? The two outputs disagree with how many lines are comments. Which line is the questionable one?

If we say that with break #3, the code doesn't stop until its found a hash or the end of the line, what can we say about the behavior using break #4?

---

While it's important that you understand the differences above, let's have a short break and take a look at something else:

Currently your code identifies which line is a comment by printing out the following:

Code:
This line is a comment because it starts with a '#'
But, we are actually interested in lines that aren't comments. How would you change your code so that it prints out something like (this is change 1 of 2):

Code:
This line is not comment because it starts with a '%c'
Where %c is the first non-space character of the line. Let's go with break #4 for now.

Last edited by phil.d.g; 10-22-2019 at 03:18 AM.
 
Old 10-22-2019, 05:11 AM   #67
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
Quote:
Originally Posted by phil.d.g View Post
Let's forget about printing lines for a minute. Does the version with break #4 consider 'no#comment' a comment?
No

Quote:
The two outputs disagree with how many lines are comments. Which line is the questionable one?
If I'm understanding you correctly, the "no#comment" line?

Quote:
If we say that with break #3, the code doesn't stop until its found a hash or the end of the line, what can we say about the behavior using break #4?
That it's only looking at the first non-space character, so in the case of the "no#comment" line, it's seeing the "n" and therefore isn't recognizing it as a comment. But it's not looking for anymore than the first non-space character either, because of the break labeled 4.

Quote:
...
Currently your code identifies which line is a comment by printing out the following:

Code:
This line is a comment because it starts with a '#'
But, we are actually interested in lines that aren't comments. How would you change your code so that it prints out something like (this is change 1 of 2):

Code:
This line is not comment because it starts with a '%c'
Where %c is the first non-space character of the line. Let's go with break #4 for now.
While I think I understand why you say to go with the break labeled 4; I'm not following what you mean by the "how I would change the code" part of what you said above - other than we're interested in the lines that aren't comments.
 
Old 10-23-2019, 12:11 AM   #68
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,271

Rep: Reputation: 152Reputation: 152
Quote:
Originally Posted by jsbjsb001 View Post
That it's only looking at the first non-space character, so in the case of the "no#comment" line, it's seeing the "n" and therefore isn't recognizing it as a comment. But it's not looking for anymore than the first non-space character either, because of the break labeled 4.
It sounds to me like you understand. I know it might be hard, but are you able to articulate why you're not convinced?

Quote:
Originally Posted by jsbjsb001 View Post
While I think I understand why you say to go with the break labeled 4; I'm not following what you mean by the "how I would change the code" part of what you said above - other than we're interested in the lines that aren't comments.
The end goal is to print all the lines in the file except for those that we consider a comment. Currently we have a program that prints a message when it finds a line with a comment. I want you to change the program so that it prints a message when it finds a line that's not a comment (so the opposite of what it does now).
 
1 members found this post helpful.
Old 10-23-2019, 02:55 AM   #69
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
Quote:
Originally Posted by phil.d.g View Post
...I know it might be hard, but are you able to articulate why you're not convinced?
I'll try, but I might not make much sense, as I'm not really sure how to explain it, but I'll try. It's not really that "I'm not convinced" per se, it's just with the program only printing the first character found, it was hard to see with the if statements there as well why break 4 made sense rather than break 3. I think it was because I was forgetting once the for loop stops it goes back to the while loop to read in another line from the file. I think I get it now though, sorry for the confusion.

Quote:
The end goal is to print all the lines in the file except for those that we consider a comment. Currently we have a program that prints a message when it finds a line with a comment. I want you to change the program so that it prints a message when it finds a line that's not a comment (so the opposite of what it does now).
I think I've done what you asked below, so here it is;

Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
          for ( i = 0; content[i] != '\0'; i++ ) {
       //          printf("%i < %i (%c) ? %s\n", i, content[i], content[i], i<content[i] ? "True" : "False");   
                    // break; // (1)  for loop stops the for loop before it starts                                                                         
              if ( content[i] != ' ' ) {                       
          //     break; // (2)   for loop stops after first char found that's not equal to a space
                 if ( content[i] != '#' ) {                               
                    printf("This line is not a comment: %s\n", content);
              //    break; // (3) for loop stops after it is not equal to a space but finds a hash. If the if statement is not equal to a hash, it displays each char following a space.
                 }
                        break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
              }
                  //   break; // (5)               
                  
          }                
    }
    
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
Here's the output;

Code:
james@jamespc: practice> ./skip_line_if_comment_just_no_comment
This line is not a comment: No comment

This line is not a comment:  No comment

This line is not a comment:                         no# comment
Thanks again!
 
Old 10-23-2019, 04:21 AM   #70
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,271

Rep: Reputation: 152Reputation: 152
No need to apologize. I'm a software developer by trade, and have been for over 10 years. The questions I asked were designed more to get you thinking in different ways and from a different perspective, than for my own benefit/clarity. Often times the act of doing that triggers a eureka moment. When I have a problem, I will explain it to a colleague. More often than not I will solve my own problem just because I've explained it to someone else, and they're left completely baffled as I thank them mid sentence and walk off, and they've not said a word.

So, you've solved your problem!! Well done!

Next steps:
  1. The requirements say it should print out the lines from the file except for those that are comments, so you should remove "This line is not a comment: " from your printf statement.
  2. Your program is erroneously printing out blank lines between every line. You should resolve that.
  3. Go back to your original problem. 34 spaces is special. Is it clearer now why that was happening?
  4. Go back to your code on post #40 and compare with this code. How is it different? Do you understand what was wrong with that code?
  5. Run the code with break #4 and then break #3 through gdb and step through each line. See how they behave differently.


We have at least one bug. What happens if you have a line with just space characters. Does that get printed or missed? The requirements say nothing about skipping blank lines.

We also made an assumption. That each fgets(...) call will read a complete line every time. In fact, it doesn't do that. If the line is longer than the buffer fgets copies to, you'll only get a partial line. We should probably cope with that.

If you want to continue learning to code, seriously consider a course. After hours college course or similar. As great a resource as something like LQ is, the disconnected nature of it isn't the best for teaching/being taught. You'll have also noticed most people are much more interested in solving technical problems rather than teaching/coaching. Once you have a working knowledge of foundational concepts you'll find it easier and easier to expand your knowledge.
 
1 members found this post helpful.
Old 10-24-2019, 09:39 AM   #71
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
Quote:
Originally Posted by phil.d.g View Post
...
So, you've solved your problem!! Well done!
Thanks phil.d.g!

Quote:
Next steps:

The requirements say it should print out the lines from the file except for those that are comments, so you should remove "This line is not a comment: " from your printf statement.
Yeah, I got rid of that.

Quote:
Your program is erroneously printing out blank lines between every line. You should resolve that.
I think I've fixed that, well, the changes I made seem to have worked, as it doesn't print out blanks lines now. I added some in the text file it looks at, and it only seems to print lines that aren't comments or blank lines now. I wasn't sure how to get it to also ignore blank lines, so I did a search (I was thinking before that it was probably code looking for the newline character though) and low and behold, a link to this very site come up where someone else was asking the same question, and luckily for me, another member seemed to have the answer. So I adapted their answer to my program.

Quote:
Go back to your original problem. 34 spaces is special. Is it clearer now why that was happening?
Yes, I think I get it now.

Quote:
Go back to your code on post #40 and compare with this code. How is it different? Do you understand what was wrong with that code?
Was it because the third if statement wasn't nested in the other two, and/or because of the first break statement in the first if statement? Beyond that, I'm not really 100% sure.

Quote:
Run the code with break #4 and then break #3 through gdb and step through each line. See how they behave differently.
I'll do that.

Quote:
...
If you want to continue learning to code, seriously consider a course. After hours college course or similar. As great a resource as something like LQ is, the disconnected nature of it isn't the best for teaching/being taught. You'll have also noticed most people are much more interested in solving technical problems rather than teaching/coaching. Once you have a working knowledge of foundational concepts you'll find it easier and easier to expand your knowledge.
I have thought about it, but I don't really have the money at the moment, and there's other reasons why that's going to be difficult at the moment too. I think if I can get this far without doing a course, there's probably no reason why I can't learn at least a few more things - it just might take a little longer. Don't get me wrong, I can understand what you're saying, and see what you mean about people here being more interested in helping solve problems rather than "teach" though. I guess like I said, I'll just have to keep plodding along until things make more sense. It kinda reminds me of when I knew nothing about Linux, and Linux being completely new to me, although, it's taken years to learn what I know about Linux - I didn't really follow any structured learning process or read a book to learn Linux - I just kept using it, and reading other people's posts here has taught me quite a bit too.

Here's my code now;

Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
          for ( i = 0; content[i] != '\0'; i++ ) {
  //          printf("%i < %i (%c) ? %s\n", i, content[i], content[i], i<content[i] ? "True" : "False");                                                                                                
              if ( content[i] != ' ' ) {                                           
                 if ( (content[i] != '#') && (content[i] != '\n') ) {                               
                    printf("%s\n", content);                                            
                 }
              //         if ( content[i] == '\n' ) {
                 //           puts("found newline");
                  //      }
              break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
              }                  
          }                
    }    
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
Here's my test file with the blank lines added between the contents of it;

Code:
No comment
#comment 1



 No comment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                        no# comment
                        
                 #a comment
And here's the output;

Code:
james@jamespc: practice> ./skip_line_if_comment_just_no_comment
No comment

 No comment

                        no# comment
(I will change the filename for both the source code file, and the executable)

Thanks so much for all the help you've given me - I just wish I knew someone in person with both the knowledge you have, and others responding to this thread (and most of my previous programming related threads) have. Thanks so much again phil.d.g!!!
 
Old 10-24-2019, 03:21 PM   #72
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,249
Blog Entries: 18

Rep: Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991
Quote:
Originally Posted by jsbjsb001 View Post
I think I've fixed that, well, the changes I made seem to have worked, as it doesn't print out blanks lines now. I added some in the text file it looks at, and it only seems to print lines that aren't comments or blank lines now.
I don't think that is quite what Phil meant. Try changing your print line to the following and you'll see what he was referring to:
Code:
printf("line[%s]\n", content);
 
Old 10-25-2019, 08:04 AM   #73
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
I done what you said GazL, and got the output below. Are you talking about the spaces between the start of the line and the actual non-space characters in the file? Because I'm not sure what else you and/or phil.d.g mean.

Code:
james@jamespc: practice> ./skip_line_if_comment
line[No comment
]
line[ No comment
]
line[                        no# comment
]
 
Old 10-25-2019, 08:27 AM   #74
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,249
Blog Entries: 18

Rep: Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991Reputation: 2991
No, what I meant is it should look like this:
Code:
ames@jamespc: practice> ./skip_line_if_comment
line[No comment]
line[ No comment]
line[                        no# comment]
hint: it's because you're still leaving the '\n' character that fgets() reads in on the end of the string/line.


P.S. Whether you want to strip off the leading spaces on the beginning of each line is entirely up to you; you already do it while looking for the first '#' so it might make sense to be consistent and also remove them from normal lines as well, but only you know whether that is appropriate for your program and what you want to do, or not.

Last edited by GazL; 10-25-2019 at 09:14 AM.
 
2 members found this post helpful.
Old 10-25-2019, 09:01 AM   #75
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,208

Original Poster
Rep: Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659Reputation: 1659
I tried to get rid of the newline character, but I don't think I've done it right. So I'm not really sure how to get rid of it, and have my program still work the way it's been intended to.

Here's my current code;

Code:
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>

int main(void) {    
       
    char content[CONTENT_LEN];
    char filename[10] = "test.txt";    
    int i = 0;    
    
    FILE *testfile; 

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }          
   
    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {        
          for ( i = 0; content[i] != '\0'; i++ ) {
             //          printf("%i < %i (%c) ? %s\n", i, content[i], content[i], i<content[i] ? "True" : "False");                                                                                                
              if ( content[i] != ' ' ) {
                 if ( content[i] == '\n' ) {
                    content[i] = '\0';
                 }
                 if ( (content[i] != '#') && (content[i] != '\0') ) {                               
                    printf("line[%s]\n", content);                                            
                 }
              //         if ( content[i] == '\n' ) {
                 //           puts("found newline");
                  //      }
              break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
              }                  
          }                
    }    
 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");
    
    fclose(testfile);
    
    return 0;
}
It still gives the same output as I posted above.
 
  


Reply

Tags
fgets, strings


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Tabs or spaces? Spaces, obviously, but how many? LXer Syndicated Linux News 0 09-13-2018 10:50 AM
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 03:16 AM
LXer: Special mention for Special purpose LXer Syndicated Linux News 0 04-22-2011 12:11 PM
renaming files with spaces and special characters. bowens44 Linux - Newbie 8 06-29-2009 07:52 PM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration