Article 4S0XJ What is so special about 34 or more spaces when reading text files with C code?

What is so special about 34 or more spaces when reading text files with C code?

by
jsbjsb001
from LinuxQuestions.org on (#4S0XJ)
So basically I've written a little program that uses fgets() to loop through each line in a text file with a while loop, but the while loop also has a nested for loop to scan trough the array that holds each string read by fgets(). Which will look for the hash symbol which will indicate a comment, and therefore the nested for loop will continue and set my bool flag to "false", because only if that flag is "true" will the printf() statement in my while loop be executed (which is what I intended).

I will admit that I got probably every error under the sun trying to get this program working even to just actually print whatever doesn't have the hash symbol in front of it - even got a "Bus Error" (don't know how I managed that, but that's new one on me). Anyhow, I got it working and fixed the problems I was having after hours sitting there trying to figure out how to get bloody thing working. But there's one small problem, as usual...

Even if I have the hash symbol in front of something that should be ignored as a comment (because it has the hash symbol in front of it), if there is more than 33 spaces (not tabs, spaces) between the start of the line, and the actual string; the string doesn't get ignored, and still gets displayed. But as long as there isn't anymore than 33 spaces between the start of the line and the string, it's fine, and the string gets ignored (as intended) where the hash symbol is in front of the string to be "ignored as a comment". I've tried searching for an answer, but either this is a very unusual problem, or I don't understand the solution, either way, I have no idea what the problem even just might be.

Here's my code (I've got the commented out for loop there because I was checking if it was actually reading the spaces, and the actual string, and on both counts, it was);

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

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(void) {

// char c;
char content[373];
char filename[10] = "test.txt";
bool str = true;
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, sizeof(content), testfile) != NULL ) {
// for ( int j = 0; (c = fgetc(testfile)) != EOF; j++ ) {
// printf("content = %c\n", content[j]);
// }
for ( i = 0; i < content[i]; i++ ) {
if ( content[i] == '#' ) {
str = false;
printf("found #\n");
continue;
}
}
if (str) {
str = true;
printf("Uncommented lines in file: %s\n", content);
}
str = true;
}

fclose(testfile);

return 0;
}This is the text file that program is reading from, where there is more than 33 spaces between the start of the "#comment 4" line, and that same string itself;

Code:line 1
#comment 1
line 3
# comment 2
line 5
# comment 3
#comment 4
#comment 5This is the output, which other than the "#comment 4" line, is correct and intended;

Code:james@jamespc: practice> ./skip_line_if_comment
Uncommented lines in file: line 1

found #
Uncommented lines in file: line 3

found #
Uncommented lines in file: line 5

found #
Uncommented lines in file: #comment 4

found #But if there is no more than 33 spaces from the start of the line to the actual string in the same text file (test.txt), then there's no issues, even with spaces between the hash symbol and the string to be commented out and ignored;

Code:james@jamespc: practice> ./skip_line_if_comment
Uncommented lines in file: line 1

found #
Uncommented lines in file: line 3

found #
Uncommented lines in file: line 5

found #
found #
found #Any help would be good, thanks.

Jameslatest?d=yIl2AUoC8zA latest?i=Ycij7XRO7xA:9Xs8Dr2hllo:F7zBnMy latest?i=Ycij7XRO7xA:9Xs8Dr2hllo:V_sGLiP latest?d=qj6IDK7rITs latest?i=Ycij7XRO7xA:9Xs8Dr2hllo:gIN9vFwYcij7XRO7xA
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments