By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

fgets info

P: n/a
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

Jun 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Salvatore Di Fazio" <sa***************@gmail.com> writes:
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.
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Jun 22 '06 #2

P: n/a
Salvatore Di Fazio wrote:

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 */

--
pete
Jun 22 '06 #3

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> writes:
"Salvatore Di Fazio" <sa***************@gmail.com> writes:
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.
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 22 '06 #4

P: n/a
A man of few words but much source.
Thank you :)

pete wrote:

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.


Jun 23 '06 #5

P: n/a
Keith Thompson wrote:
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"
:)

Jun 23 '06 #6

P: n/a
I forgot

I resolved just with isspace();

Thank you everybody

Jun 23 '06 #7

P: n/a
Salvatore Di Fazio wrote:

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:Th*************@gmail.com>
Jun 24 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.