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.

while(fscanf("%s",temp.name) != EOF)
{

temp = CreateNode();
setData();
Insert();

}

:cheesy:

any suggestions with reading the information from the file ,and creating the linked list? thanks

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...

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

fgets(temp ->head.name,50,ifp);
   fgets(temp ->head.foodType,50,ifp);
   fgets(temp ->head.servingSize,50,ifp);

   fscanf(ifp,"%d",&temp ->head.numOfCals);
   fscanf(ifp,"%d",&temp ->head.amountOfSodium);
   fscanf(ifp,"%f",&temp ->head.gramsOfFat);
   fscanf(ifp,"%d",&temp ->head.gramsOfChol);
   fscanf(ifp,"%f",&temp ->head.gramsOfSatFat);
   fscanf(ifp,"%d",&temp ->head.gramsOfCarbs);
   fscanf(ifp,"%d",&temp ->head.gramsOfSug);
   fscanf(ifp,"%d",&temp ->head.gramsOfFib);
   fscanf(ifp,"%d",&temp ->head.gramsOfProt);

and the output is(which is correct)

Roasted Turkey Breast
MEAT
3 oz.
160
55
6.000000
65
2.000000
0
0
0
24

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

RoastedTurkeyBreast0
0
0.000000
0
0.000000
0
0
0
0

:o

thanks for your help

This article has been dead for over six months. Start a new discussion instead.