Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fgets info

Reply
Thread Tools

fgets info

 
 
Salvatore Di Fazio
Guest
Posts: n/a
 
      06-22-2006
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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      06-22-2006
"Salvatore Di Fazio" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-22-2006
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-22-2006
Ben Pfaff <(E-Mail Removed)> writes:
> "Salvatore Di Fazio" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Salvatore Di Fazio
Guest
Posts: n/a
 
      06-23-2006
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.


 
Reply With Quote
 
Salvatore Di Fazio
Guest
Posts: n/a
 
      06-23-2006
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"


 
Reply With Quote
 
Salvatore Di Fazio
Guest
Posts: n/a
 
      06-23-2006
I forgot

I resolved just with isspace();

Thank you everybody

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      06-24-2006
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: <(E-Mail Removed)>


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[URGENT] fgets reading last line in file twice DJP C++ 7 10-21-2004 09:23 AM
fgets and newline Mike Mimic C++ 4 05-15-2004 06:19 AM
cin before fgets Charles Wilkins C++ 4 08-07-2003 10:14 PM
fgets and problems reading into array Eigenvector C Programming 12 07-29-2003 02:20 AM
Problems with fgets and reading in a number FakeAddress@NoSpam.com C Programming 6 06-25-2003 09:18 PM



Advertisments