| | |
C/ Need Help with Linked Lists please
![]() |
•
•
Join Date: Jul 2004
Posts: 17
Reputation:
Solved Threads: 0
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.
:cheesy:
any suggestions with reading the information from the file ,and creating the linked list? thanks
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.
C Syntax (Toggle Plain Text)
while(fscanf("%s",temp.name) != EOF) { temp = CreateNode(); setData(); Insert(); }
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...
If not you'll have to write some code to detect what the input actually is and call the correct routine based on that...
•
•
Join Date: Jul 2004
Posts: 17
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
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)
[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
![]() |
Similar Threads
- strcpy and linked lists (C++)
- Concordance, Linked Lists and classes (C++)
- help with linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- stack of linked lists (C++)
Other Threads in the C Forum
- Previous Thread: setting the number of decimal points in double varibale
- Next Thread: Super soccer league c program
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






