fgets info

Discussion in 'C Programming' started by Salvatore Di Fazio, Jun 22, 2006.

  1. 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
     
    Salvatore Di Fazio, Jun 22, 2006
    #1
    1. Advertising

  2. Salvatore Di Fazio

    Ben Pfaff Guest

    "Salvatore Di Fazio" <> 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
     
    Ben Pfaff, Jun 22, 2006
    #2
    1. Advertising

  3. Salvatore Di Fazio

    pete Guest

    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
     
    pete, Jun 22, 2006
    #3
  4. Ben Pfaff <> writes:
    > "Salvatore Di Fazio" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 22, 2006
    #4
  5. 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.
     
    Salvatore Di Fazio, Jun 23, 2006
    #5
  6. 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"
    :)
     
    Salvatore Di Fazio, Jun 23, 2006
    #6
  7. I forgot

    I resolved just with isspace();

    Thank you everybody
     
    Salvatore Di Fazio, Jun 23, 2006
    #7
  8. 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:>
     
    Kenneth Brody, Jun 25, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vasanth
    Replies:
    0
    Views:
    561
    Vasanth
    Jun 28, 2004
  2. Andrea Williams

    Clear the Render info & change to different info

    Andrea Williams, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,424
    Ken Dopierala Jr.
    Oct 27, 2004
  3. Kiran
    Replies:
    0
    Views:
    1,228
    Kiran
    Feb 6, 2005
  4. ilya

    get CPU info, RAM info

    ilya, Apr 8, 2010, in forum: Java
    Replies:
    43
    Views:
    6,017
    Mike Schilling
    Apr 19, 2010
  5. Kenshin
    Replies:
    1
    Views:
    226
    Jeff Cochran
    Jun 25, 2004
Loading...

Share This Page