Hi guys, I have a real quick problem, or at least I hope it's really quick. The thing is, I'm studying for a computing C final, and one of the sample questions requires me to take data from a file, put it in a linked list, and then place it is a file. The actual question reads:

2) Use linked list to read data from file

The file contains 4 columns per line.
For example,
2009-11-20 125 250 350

First is date, second is breakfast, third is lunch, fourth is dinner.

If there's data already in the person's file, then read all into a linked list. Each line in the file corresponds to one node in the linked list.
Use the following node structure.

     struct calorie_day {
        char date[11];     
        int breakfast;      
        int lunch;        
        int dinner;       
        struct calories *next; //pointer for next day
     }; 

If there's no data in file, the linked list should be empty.

3) Save data to file
When program exits, save all data in linked list to the person's file.
The file should contain all 4 columns per line.
It should be sorted by date.

     2009-11-20 125 250 350
     2009-11-21 130 150 400
     2009-11-22 0 330 240 

Now, even after a semester of C, I'm still a little lost when ti comes to linked lists and files because they were crammed into the last three lectures. Could you guys help me out please? Post actual code if you have to, but can you please try to help me learn rather than just giving me the answer? The final is going to tear me a new one as it is haha :P

Thank you so much.

Sorry...after reading the rules and such I realize that I should have added what I currently have. Thanks for the help guys.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
          char date[11];     // format is 2009-11-20
          int breakfast;     // calories for breakfast
          int lunch;         // calories for lunch
          int dinner;        // calories for dinner
          struct node *next;  // pointer to next day
          };

typedef struct node NODE;


int main(int argc, char *argv[])
{


NODE *makeNewNode()
{
FILE *charname;
charname=fopen(argv[1],"r");

if (charname == NULL)
        {
                printf("Please put in an existing file.\n");
                return;
        }


NODE *newnode;

newnode=malloc(sizeof(NODE));
newnode->next=NULL;

fscanf(charname, "%s %d %d %d", newnode->date, &newnode->breakfast, &newnode->lunch, &newnode->d
inner);

fprintf(stdout, "%s %d %d %d", newnode->date, newnode->breakfast, newnode->lunch, newnode->dinne
r);



fclose(charname);
}



return 0;

The thing is, after this point I'm stuck. I think that I have my linked list reading from a file, and I think that I have it writing to one, but I don't know how to check, or how to go to the next line if a file has more than one date in it...

Good luck on the final man.

I'm pretty sure I'm in your class. ...

I would do the linked lists a bit different.

I would use global pointers
NODE *ptr1, *ptr2, ...etc

The use of pointers is so that you can keep track of what the previous node is.

So say you created a new node. How would you know the address of the previous node so that you can link the two together?

So you created one node, let's say hypothetically at address 1.

Then you create another one at address 100.

Since you don't hardcode the addresses down, the point is that the *next in the first node should point to the second node, address 100. You would not be able to assign the address to the first node if you don't know where the first node is.

Here it is pictorally:

step 1: created node
newnode (points to) node 1
assigned next to Null

step 2: create another node
newnode (now points to) node 2
assigned next to NULL
// How do we get node 1 -> next to point to node 2?

we have to do this:
step 0: create pointer

step 1: created node
newnode (points to) node 1
assigned next to Null
pointer points to newnode

step 2: create another node
newnode (now points to) node 2
assigned next to NULL
pointer still points to first node, so you can assign
pointer->next = newnode

Finally, your approach to the problem is sort of... whacky

All your code seems to be in one section, instead of sectioned into functions, which would make it conceptually easier.

just check what you really want to do. You should open the file OUTSIDE of the make new node function, as well as close it outside. The way you have it, everytime you make a new node, you open and close the file.

If you leave the file open, everytime it runs fscanf it will move to the next line, so you don't really have to do any filepointer moving.

As for your malloc... you need to typecast the malloc since malloc always gives a void pointer. So what you need is newnode = (NODE *)malloc(sizeof(NODE));
and you should check if it returned NULL as well.

The next step in your program should be to make a loop so that the make new node runs until it reaches the end of the file.

You should come up with specific problems to ask since a general "help me" is kind of hard.

Good luck.

PS

and you are writing to stdout, which is the monitor, so you are not writing to file. You have to write to the filepointer if you want to write to the file, eg fprintf(fp, "blahblah%d", day);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.