C/ Need Help with Linked Lists please

Reply

Join Date: Jul 2004
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

C/ Need Help with Linked Lists please

 
0
  #1
Dec 3rd, 2004
Hello,

I have a project which involves basic implementations of linked lists, in C.

First though, I have to read the information from a file, which contains all different types(int, char, float) which is my problem...after i read the information into the nodes, it should be smooth sailing..

I was thinking something like...for example the first thing in the file is a name(string), but the next is an int...so I wasnt sure how to get the int after the first string.



  1. while(fscanf("%s",temp.name) != EOF)
  2. {
  3.  
  4. temp = CreateNode();
  5. setData();
  6. Insert();
  7.  
  8. }
:cheesy:

any suggestions with reading the information from the file ,and creating the linked list? thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: C/ Need Help with Linked Lists please

 
0
  #2
Dec 4th, 2004
if you know the order of the data it's as simple as hardcoding the different input conversions in that order. For int that would be a call to atoi which converts a string into an int.
If not you'll have to write some code to detect what the input actually is and call the correct routine based on that...
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

Re: C/ Need Help with Linked Lists please

 
0
  #3
Dec 5th, 2004
okay i see what your saying, and I do already know what order the input is, so that makes it easier...so i have something like this...and it does work

  1.  
  2. fgets(temp ->head.name,50,ifp);
  3. fgets(temp ->head.foodType,50,ifp);
  4. fgets(temp ->head.servingSize,50,ifp);
  5.  
  6. fscanf(ifp,"%d",&temp ->head.numOfCals);
  7. fscanf(ifp,"%d",&temp ->head.amountOfSodium);
  8. fscanf(ifp,"%f",&temp ->head.gramsOfFat);
  9. fscanf(ifp,"%d",&temp ->head.gramsOfChol);
  10. fscanf(ifp,"%f",&temp ->head.gramsOfSatFat);
  11. fscanf(ifp,"%d",&temp ->head.gramsOfCarbs);
  12. fscanf(ifp,"%d",&temp ->head.gramsOfSug);
  13. fscanf(ifp,"%d",&temp ->head.gramsOfFib);
  14. fscanf(ifp,"%d",&temp ->head.gramsOfProt);

and the output is(which is correct)
[PHP]
Roasted Turkey Breast
MEAT
3 oz.
160
55
6.000000
65
2.000000
0
0
0
24
[/PHP]

and I'm going to have to have a while loop which keeps creating new nodes until it hits the EOF...but thats another story...so for now....
i have another longer question but I can figure it out myself, if someone could tell me if fscanf() can read in strings? because when i tried to use fscanf for the first three items in the lists which are not ints or floats, but are strings it doesnt work i did something like fscanf("%s",temp -> head.name);
and the output is
[PHP]
RoastedTurkeyBreast0
0
0.000000
0
0.000000
0
0
0
0
[/PHP]
:o

thanks for your help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC