Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

files + records in c

Options
  • 31-03-2003 3:51pm
    #1
    Registered Users Posts: 2,593 ✭✭✭


    i am having trouble when i reading a record from a file the first record that is read is "garbage" then the rest are ok could someone tell me what going on and an idea on how to solve it...
    tanx


    struct vertexnode{

    char SiteName[20];
    int XCoord;
    int YCoord;
    struct vertexnode *nextvertex;
    struct arcnode *nextarc;
    };


    void saveGraph(vertexnode *graph)
    {
    FILE *fp;

    fp = fopen("graph.txt","wt");
    while (graph!=NULL)
    {
    fwrite(&*graph,sizeof (*graph),1,fp);
    graph=graph->nextvertex;
    }
    fclose(fp);

    }

    //*****************************************************************************

    vertexnode *loadGraph(void)
    {
    FILE* fp;
    vertexnode* ptr = NULL;
    long curpos;


    fp = fopen("graph.txt","rt");
    curpos = ftell(fp);
    fseek(fp, curpos, SEEK_SET);
    fflush(fp);

    while (!feof(fp))
    {
    vertexnode* newnode;
    newnode = GetvertexNode();
    newnode->nextvertex = NULL;
    newnode->nextarc = NULL;
    fread(&*newnode,sizeof (*ptr),1,fp);
    newnode->nextvertex = ptr;
    ptr = newnode;
    newnode->SiteName;
    newnode->XCoord;
    newnode->YCoord;
    }

    fclose(fp);
    return (ptr);
    }


Comments

  • Registered Users Posts: 6,240 ✭✭✭hussey


    have a look at this threadhttp://www.boards.ie/vbulletin/showthread.php?s=&threadid=75924&highlight=struct, its in C++ , but can give you an idea of how to write/read structs from files


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    [php]struct vertexnode
    {
    char SiteName[20];
    int XCoord;
    int YCoord;
    struct vertexnode *nextvertex;
    struct arcnode *nextarc;
    };

    void saveGraph(vertexnode *graph)
    {
    FILE *fp;

    fp = fopen("graph.txt","wt");
    while (graph!=NULL)
    {
    // fwrite(&*graph,sizeof (*graph),1,fp);
    fwrite(graph, sizeof (vertexnode),1,fp); // BETTER
    graph=graph->nextvertex;
    }
    fclose(fp);

    }[/php]


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    sorry guys
    still have the prob it seems to be trying to read the last node twice????


Advertisement