fscanf help

This is a discussion on fscanf help within the C Programming forums, part of the General Programming Boards category; I, and a group of 5 other people, are writing a database for a final project in c. When we ...

  1. #1
    Unregistered
    Guest

    fscanf help

    I, and a group of 5 other people, are writing a database for a final project in c. When we try to read from our file database.txt, the entire code compiles but our scan function causes the program to crash when selected from our user menu. Here is the fscanf function we wrote:

    node *fscan(node *head) // Save to file Function
    {
    FILE *ofp;
    node *t;
    node *headnode = NULL;
    t = (node *) malloc (sizeof (node));



    if((ofp=fopen("H:\\database.txt", "r"))==NULL) // open for text read
    {
    printf("Cannot open file. There are no records to display\n");
    exit(1);
    }

    while(!feof(ofp))
    {
    fscanf(ofp,"%s %s %f %d %s %d %s %d %d %d %d",
    t->first_name,
    t->last_name,
    &t->b_average,
    &t->rbi,
    t->team,
    &t->homeruns,
    t->position,
    &t->hits,
    &t->games_played,
    &t->seasons_played,
    &t->strike_outs);


    t->nextnode = NULL;
    head = build_list(head, t);
    t=t->nextnode;
    }

    return head;
    fclose(ofp);

    I think we are having a memory problem b/c when the comp starts up, fscanf will run once or twice, but after that we get a memory error when this function is selected. Sorry for the long post but this is due 4/30/2002 and we didn't know what else to do.Thanks.

    dennis

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: fscanf help

    Code:
    t = (node *) malloc (sizeof (node));
    Do some error checking here, t isn't guaranteed to be usable.

    Code:
     
      return head;
      fclose(ofp);
    No point in having code after you've return'ed ! You wanna swap these two around?

    Are you sure it's failing when doing fscanf, or is it failing elsewhere with dynamic memory alloc usage problems? Put some printf's before and after the fscanf to see if this is the case.


    Code:
    printf("Doing fscanf\n");
    /* do fscanf bit */
    printf("Done fscanf\n");
    Lastly, I don't *think* you should be using feof to test for EOF. You could use the return code from fscanf, which is the number of items read (11 for a good read on your code), or EOF at end of file.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,648
    Well, first of all, if your struct node's char[] members are pointers are pointers, then you need to allocate memory for them, otherwise, just make sure the array is big enough...



    Code:
    
    node *fscan(node *head) // Save to file Function 
    { 
    FILE *ofp; 
    
    
    if((ofp=fopen("H:\\database.txt", "r"))==NULL) // open for text read 
    { 
    printf("Cannot open file. There are no records to display\n"); 
    exit(1); 
    } 
    
    
    node *go = head;
    
    
    
    while(!feof(ofp)) 
    { 
    node *t = (node *) malloc (sizeof (node)); ; 
    
    t->nextnode = NULL;
    
    go->nextnode = t;
    
    t->prevnode = go;
    
    go = go->nextnode;
    
    fscanf(ofp,"%s %s %f %d %s %d %s %d %d %d %d", 
    t->first_name, 
    t->last_name, 
    &t->b_average, 
    &t->rbi, 
    t->team, 
    &t->homeruns, 
    t->position, 
    &t->hits, 
    &t->games_played, 
    &t->seasons_played, 
    &t->strike_outs); 
    } 
    
    fclose(ofp); 
    
    
    return head; 
    }

    ...may work barring any other problems...
    Last edited by Sebastiani; 04-28-2002 at 04:11 PM.
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  4. #4
    Unregistered
    Guest
    Thanks we finally figured it out. Appreciate the quick responses.

    Dennis

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    Thanks we finally figured it out. Appreciate the quick responses.

    Dennis
    And the answer was...?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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