Fgets problem again

This is a discussion on Fgets problem again within the C Programming forums, part of the General Programming Boards category; Hi I wasnt sure if I should continue to post in the previous thread, but I overlooked a specification, and ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Fgets problem again

    Hi

    I wasnt sure if I should continue to post in the previous thread, but I overlooked a specification, and I couldnt get it to work with this.

    I was using the suggestion if line[0] = '.' to stop reading, but that doesnt work if there is text after it. (ie. .blah blah blah), it wouldnt read the blah blah blah.

    I should, however, stop reading when there is a period is on a line by itself, as the first character.

    Also, if there is a period to begin the line, I have to add a period to the beginning of the line.

    I know I can prepend the period with strcat, but Im not sure how to structure the while loop.

    Any suggestions would again be appreciated.

    Thanks

    (that is how the file looks, with records as below)
    Date: Feb 20, 2004
    Requestor: Paul
    Email: bob@yahoo.com
    Priority: Medium
    Needed: now
    Status: ok
    Description:
    Test here.
    .
    Last edited by MethodMan; 02-28-2004 at 10:32 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An idea.
    Code:
    /* test.txt
    Date: Feb 20, 2004
    Requestor: Paul
    Email: bob@yahoo.com
    Priority: Medium
    Needed: now
    Status: ok
    Description:
    Test here.
    .
    Date: Feb 20, 2004
    Requestor: Paul
    Email: doug@yahoo.com
    Priority: Medium
    Needed: now
    Status: ok
    Description:
    Test here.
    And here.
    And here.
    .
    */
    #include <stdio.h>
    
    struct record
    {
       char  date        [ BUFSIZ ],
             requestor   [ BUFSIZ ],
             email       [ BUFSIZ ],
             priority    [ BUFSIZ ],
             needed      [ BUFSIZ ],
             status      [ BUFSIZ ],
             description [ BUFSIZ ];
    };
    
    int foo(FILE *file, char *dst, size_t size)
    {
       size_t i;
       for ( i = 0; i < size; ++i )
       {
          int ch = fgetc(file);
          if ( ch == EOF )
          {
             return 0;
          }
          dst[i] = ch;
          if ( i > 2 && dst[i - 3] == '.' && dst[i - 2] == '\n' &&
               dst[i - 1] == '.' && dst[  i  ] == '\n' )
          {
             break;
          }
       }
       dst[i - 1] = '\0';
       return 1;
    }
    
    int main(void)
    {
       const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          struct record record;
          const char format[] = "Date: %[^\n]%*c"
                                "Requestor: %[^\n]%*c"
                                "Email: %[^\n]%*c"
                                "Priority: %[^\n]%*c"
                                "Needed: %[^\n]%*c"
                                "Status: %[^\n]%*c"
                                "Description: ";
          while ( fscanf(file, format, 
                         record.date, 
                         record.requestor, 
                         record.email, 
                         record.priority, 
                         record.needed, 
                         record.status) == 6 )
          {
             printf("Date:      %s\n", record.date);
             printf("Requestor: %s\n", record.requestor);
             printf("Email:     %s\n", record.email);
             printf("Priority:  %s\n", record.priority);
             printf("Needed:    %s\n", record.needed);
             printf("Status:    %s\n", record.status);
             /*
              * fscanf did some rough stuff, but now we've got some work to do.
              */
             if ( foo(file, record.description, sizeof record.description) )
             {
                printf("Description: \n%s\n", record.description);
             }
          }
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    Date:      Feb 20, 2004
    Requestor: Paul
    Email:     bob@yahoo.com
    Priority:  Medium
    Needed:    now
    Status:    ok
    Description: 
    Test here.
    
    Date:      Feb 20, 2004
    Requestor: Paul
    Email:     doug@yahoo.com
    Priority:  Medium
    Needed:    now
    Status:    ok
    Description: 
    Test here.
    And here.
    And here.
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Code:
                    fgets(descline, sizeof(descline),fp);
                    
                    while( (strncmp(descline, ".",1)) != 0)
                    {
                            strcat(description, descline);
                            fgets(descline, sizeof(descline),fp);
                    }
    I was trying this, which doesnt include the code to prepend the period.

    If I get to something in the file like:

    This is some text
    .this is some text.
    .this is more.
    .

    It wont read lines beginning with a period.

    How can I fix this?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >How can I fix this?

    Did you try the code I posted?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Yes I have.

    I appreciate the time you took to write that up, but I was hoping someone could explain why mine doesnt work.

    I would also like to fully understand the code I use...

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    strncmp(descline, ".",1)
    If you intend to compare to more than one character, then compare to more than one character?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 11:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 10:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21