fgets info

S

Salvatore Di Fazio

I need to check if the line that I read in a file is a empty line or a
comment,
so I did:

if (line[0] != '#' && line[0] != ' ' && (line[0] != '\r' || line[0]
!= '\n'))

but if I have a file like this:

1: # comment
2: # comment
3:
4:
5:
6: # comment

the line number 3 it's not jumped so the program crashs

Any help?
tnx
 
B

Ben Pfaff

Salvatore Di Fazio said:
I need to check if the line that I read in a file is a empty line or a
comment,
so I did:

if (line[0] != '#' && line[0] != ' ' && (line[0] != '\r' || line[0]
!= '\n'))

The test (line[0] != '\r' || line[0] != '\n') is always true.
but if I have a file like this:

1: # comment
2: # comment
3:
4:
5:
6: # comment

the line number 3 it's not jumped so the program crashs

I don't know why a blank line would cause your program to crash.
You haven't given us enough information to tell.
 
P

pete

Salvatore said:
I need to check if the line that I read in a file is a empty line or a
comment,
so I did:

if (line[0] != '#' && line[0] != ' '
&& (line[0] != '\r' || line[0] != '\n'))

but if I have a file like this:

1: # comment
2: # comment
3:
4:
5:
6: # comment

the line number 3 it's not jumped so the program crashs

comment.txt is open for reading.
line 1 is not blank.
line 2 is not blank.
line 6 is not blank.
comment.txt is closed.


/* BEGIN comment.c */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>

#define SOURCE "comment.txt"

int blank(char *line);
int text_line(FILE *fp, char **line, size_t *size);
int line2string(FILE *fp, char **line, size_t *size);

int line_count;

int main(void)
{
FILE * fp;
char *line;
size_t size;
int rc;

line = NULL;
size = 0;

fp = fopen(SOURCE, "r");
if (fp != NULL) {
puts(SOURCE " is open for reading.");
rc = text_line(fp, &line, &size);
while (rc > 1) {
printf("line %d is not blank.\n", line_count);
rc = text_line(fp, &line, &size);
}
fclose(fp);
puts(SOURCE " is closed.\n");
if (rc == 0) {
puts("realloc problem");
exit(EXIT_FAILURE);
}
} else {
puts(SOURCE " not opened.");
}
return 0;
}

int blank(char *line)
{
unsigned char c = *line;

while (isspace(c) || c != '\0' && !isprint(c)) {
c = *++line;
}
return c == '\0';
}

int text_line(FILE *fp, char **line, size_t *size)
{
int rc;

do {
++line_count;
rc = line2string(fp, line, size);
} while (rc > 0 && blank(*line));
return rc;
}

int line2string(FILE *fp, char **line, size_t *size)
{
int rc;
void *p;
size_t count;

count = 0;
for (rc = getc(fp); rc != EOF; rc = getc(fp)) {
++count;
if (count + 2 > *size) {
p = realloc(*line, count + 2);
if (p == NULL) {
if (*size > count) {
(*line)[count] = '\0';
(*line)[count - 1] = (char)rc;
} else {
ungetc(rc, fp);
}
count = 0;
break;
}
*line = p;
*size = count + 2;
}
if (rc == '\n') {
(*line)[count - 1] = '\0';
break;
}
(*line)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = count > INT_MAX ? INT_MAX : count;
} else {
if (*size > count) {
(*line)[count] = '\0';
}
}
return rc;
}

/* END comment.c */
 
K

Keith Thompson

Ben Pfaff said:
Salvatore Di Fazio said:
I need to check if the line that I read in a file is a empty line or a
comment,
so I did:

if (line[0] != '#' && line[0] != ' ' && (line[0] != '\r' || line[0]
!= '\n'))

The test (line[0] != '\r' || line[0] != '\n') is always true.

And why are you checking for '\r' anyway? If you're on a system that
uses "\r\n" to represent end-of-line, and you opened the file in text
mode, each end-of-line sequence will be translated to a single '\n'
character.

If you're checking for, say, Windows-format text files on a Unix
system, then checking for '\r' might be reasonable -- but it would
probably be better to convert the file to a real local-format text
file before trying to read it.

You also need to define exactly what you mean by a "comment", and by
an "empty line". For a comment, does the '#' have to be the first
character in the line? You're also checking whether the first
character is ' ', which doesn't seem to have anything to do with your
problem statement.
I don't know why a blank line would cause your program to crash.
You haven't given us enough information to tell.

A small, complete, compilable program would probably be enough
information.
 
S

Salvatore Di Fazio

Keith said:
And why are you checking for '\r' anyway? If you're on a system that
uses "\r\n" to represent end-of-line, and you opened the file in text
mode, each end-of-line sequence will be translated to a single '\n'
character.

I did that just because I thought "if one line is empty I find \r \n or
just \n in Unix"
:)
 
K

Kenneth Brody

Salvatore said:
I forgot

I resolved just with isspace();

What did you resolve with isspace(), and what will happen if the
first character of the line is a space character?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top