fscanf reading lines

Discussion in 'C Programming' started by bhanuprakash@gmail.com, Mar 8, 2006.

  1. Guest

    I am trying to use fscanf to read my test file. In my test file i
    sometimes have blank lines.
    When I try to read using the following format.

    fscanf(fp,"%[^\n]\n",temp_str);

    If there is any blank line it reads contents from the next line. How do
    I read blank into my variable if the line is blank.

    Example

    Line Number Input
    1 Hi
    2
    3 Smile

    In the above scenario if I try to read line number 2 where it is blank
    it reads "Smile" into my temp_sttring variable. How do I ensure if
    there is a blank line my variable also ends up blank.
     
    , Mar 8, 2006
    #1
    1. Advertising

  2. said:

    > When I try to read using the following format.
    >
    > fscanf(fp,"%[^\n]\n",temp_str);
    >
    > If there is any blank line [fscanf] reads contents from the next line.
    > How do I read blank into my variable if the line is blank.


    Easy. Don't use fscanf.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Mar 8, 2006
    #2
    1. Advertising

  3. Guest

    Hello,

    when you are using fscanf(fp,"%[^\n]\n",temp_str); you are saying that
    the token you wish to not parse is '\n'. Since the blank line is
    uniquely composed of '\n', then it skips that line and goes to the
    next. Check the return value of fscanf to see how many values were
    read, which will tell you if there was a blank line or not.


    Joel P.
     
    , Mar 8, 2006
    #3
  4. Guest

    Hi,
    what would be the best possible solution in this scenario. i
    tried using fgets but it has another problem.
    if I want to read something like
    Line No Input
    1 SMILE

    fgets(temp_str,sizeof(temp_str),fp);

    Then in my variable temp_str it contains the value as "SMILE
    ".

    There are some additional characters appended to my variable.

    I want only the string "SMILE" come into the variable temp-str.

    Please help me on this.
     
    , Mar 8, 2006
    #4
  5. writes:
    > what would be the best possible solution in this scenario. i
    > tried using fgets but it has another problem.
    > if I want to read something like
    > Line No Input
    > 1 SMILE
    >
    > fgets(temp_str,sizeof(temp_str),fp);
    >
    > Then in my variable temp_str it contains the value as "SMILE
    > ".
    >
    > There are some additional characters appended to my variable.
    >
    > I want only the string "SMILE" come into the variable temp-str.


    You'll have to be much clearer about what you're doing.

    Are you saying that line 1 of your input file contains only the string
    "SMILE" (followed by a new-line) and nothing else?

    Show us a complete compilable program that illustrates the problem
    you're having.

    (And read <http://cfaj.freeshell.org/google/> *before* you post
    another followup.)

    --
    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, Mar 8, 2006
    #5
  6. pete Guest

    wrote:
    >
    > I am trying to use fscanf to read my test file. In my test file i
    > sometimes have blank lines.
    > When I try to read using the following format.
    >
    > fscanf(fp,"%[^\n]\n",temp_str);
    >
    > If there is any blank line it reads contents from the next line.
    > How do
    > I read blank into my variable if the line is blank.


    rc = fscanf(fp, "%[^\n]", temp_str);
    if (!feof(fp)) {
    getc(fp);
    }
    if (rc == 0) {
    *line = '\0';
    }
    >
    > Example
    >
    > Line Number Input
    > 1 Hi
    > 2
    > 3 Smile
    >
    > In the above scenario if I try to read line number 2 where it is blank
    > it reads "Smile" into my temp_sttring variable. How do I ensure if
    > there is a blank line my variable also ends up blank.


    /* BEGIN type_.c */
    /*
    ** This program uses fscanf
    ** to read lines of text files.
    */
    #include <stdio.h>
    #include <stdlib.h>

    #define ARGV_0 type_
    #define str(s) # s
    #define xstr(s) str(s)

    unsigned long max_line_len(FILE *fd);

    int main(int argc, char *argv[])
    {
    int rc;
    FILE *fd;
    char *line;
    unsigned long length;
    unsigned long line_length;

    if (argc > 1) {
    line_length = 1;
    line = malloc(line_length + 1);
    if (line == NULL) {
    fprintf(stderr, "line_length is %lu\n", line_length);
    exit(EXIT_FAILURE);
    }
    while (*++argv != NULL) {
    fd = fopen(*argv, "r");
    if (fd != NULL) {
    length = max_line_len(fd);
    if (length > line_length) {
    line_length = length;
    free(line);
    line = malloc(line_length + 1);
    if (line == NULL) {
    fprintf(stderr,
    "line_length is %lu\n", line_length);
    exit(EXIT_FAILURE);
    }
    }
    do {
    rc = fscanf(fd, "%[^\n]", line);
    if (!feof(fd)) {
    getc(fd);
    }
    switch (rc) {
    case 0:
    *line = '\0';
    case 1:
    puts(line);
    default:
    break;
    }
    } while (rc != EOF);
    fclose(fd);
    } else {
    fprintf(stderr,
    "\nfopen() problem with \"%s\"\n", *argv);
    break;
    }
    }
    free(line);
    } else {
    puts(
    "Usage:\n>" xstr(ARGV_0)
    " <FILE_0.txt> <FILE_1.txt> <FILE_2.txt> ...\n"
    );
    }
    return 0;
    }

    unsigned long max_line_len(FILE *fd)
    {
    unsigned long count, max;
    int rc;

    count = max = 0;
    rc = getc(fd);
    while (rc != EOF) {
    if (rc == '\n') {
    if (count > max) {
    max = count;
    }
    count = 0;
    } else {
    ++count;
    }
    rc = getc(fd);
    }
    rewind(fd);
    return max;
    }

    /* END type_.c */


    --
    pete
     
    pete, Mar 8, 2006
    #6
  7. pete Guest

    pete wrote:

    > line = malloc(line_length + 1);


    > rc = fscanf(fd, "%[^\n]", line);
    > if (!feof(fd)) {
    > getc(fd);
    > }


    > unsigned long max_line_len(FILE *fd)
    > {
    > rewind(fd);
    > }


    If you don't want to go through the file twice,
    and don't mind truncating long lines:

    #define LENGTH 80

    #define str(x) # x
    #define xstr(x) str(x)

    line_length = LENGTH;
    line = malloc(line_length + 1);

    rc = fscanf(fd, "%" xstr(LENGTH) "[^\n]%*[^\n]", line);
    if (!feof(fd)) {
    getc(fd);
    }

    --
    pete
     
    pete, Mar 8, 2006
    #7
  8. <> wrote in message
    news:...
    >I am trying to use fscanf to read my test file. In my test file i
    > sometimes have blank lines.
    > When I try to read using the following format.
    >
    > fscanf(fp,"%[^\n]\n",temp_str);
    >
    > If there is any blank line it reads contents from the next line. How do
    > I read blank into my variable if the line is blank.
    >
    > Example
    >
    > Line Number Input
    > 1 Hi
    > 2
    > 3 Smile
    >
    > In the above scenario if I try to read line number 2 where it is blank
    > it reads "Smile" into my temp_sttring variable. How do I ensure if
    > there is a blank line my variable also ends up blank.
    >


    If you are trying to read the last blank-delimited string:
    1. read the line using fgets
    2. (safety check) if last character in buffer is not newline, repeat
    3. replace trailing newline with '\0'
    4. use ptr=strrchr(text, ' ') to find the last blank
    5. if ptr==null the entire string is what you want
    6. if ptr != null, ++ptr points to the beginning of the desired string
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Mar 8, 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. Benedicte

    Problems when reading from a file with fscanf

    Benedicte, Feb 4, 2004, in forum: C Programming
    Replies:
    3
    Views:
    452
    Al Bowers
    Feb 4, 2004
  2. CJ

    fscanf to read lines from file?

    CJ, May 20, 2004, in forum: C Programming
    Replies:
    2
    Views:
    2,996
    Dan Pop
    May 21, 2004
  3. Psibur

    Missing Lines With fscanf()

    Psibur, Jun 4, 2004, in forum: C Programming
    Replies:
    4
    Views:
    602
    Chris Torek
    Jun 11, 2004
  4. John
    Replies:
    4
    Views:
    640
    Keith Thompson
    Sep 27, 2006
  5. V.Subramanian, India

    no error by fscanf on reading from output file

    V.Subramanian, India, Oct 30, 2011, in forum: C Programming
    Replies:
    18
    Views:
    580
    Keith Thompson
    Oct 31, 2011
Loading...

Share This Page