944,094 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3169
  • C RSS
Dec 3rd, 2004
0

C/ Need Help with Linked Lists please

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Dec 4th, 2004
0

Re: C/ Need Help with Linked Lists please

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...
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 5th, 2004
0

Re: C/ Need Help with Linked Lists please

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: setting the number of decimal points in double varibale
Next Thread in C Forum Timeline: Super soccer league c program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC