473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Linked List and file reading

Hello, I have been having a problem lately relating to a linked list example I wanted to try. For some reason, perhaps I am not saving the data to the linked list correctly OR my print function is incorrect. The two arguements to be passed are a filename and an integer (which I havent used yet). (I removed the freeLists section to save on posting space.)

To note, in the file a sample of data which would be used to read would be:
111 aaa bbb 1.1
222 ccc ddd 2.2

Which gives me an output of (which should really repeat exactly what is in the file):
ûÿ¿û ÿ=Dÿ¿ûX ÿÿÿÿÿÿÿÿ
ûÿ¿û ÿ=Dÿ¿ûX ÿÿÿÿÿÿÿÿ

----------------------code--------------------------
struct dataNodule
{
char* dataA;
char* dataB;
char* dataC;
char* dataD;

struct dataNodule *next;
};
typedef struct dataNodule dataNode;

void loadList(dataNode **startList, char* filename);
void printList(dataNode *startList, char* sortMethod);
void freeList(dataNode **startList);
dataNode* createNode(char* dataA, char* dataB, char* dataC, char* dataD);

int main(int argc, char *argv[])
{
dataNode *startPtr = NULL;

char *filename = 0, *sortMethod = 0;

if(argv[1] != NULL && argv[2] != NULL)
{
filename = argv[1];
sortMethod = argv[2];
}
else
{
exit(0);
}

loadList(&startPtr, filename);
printList(startPtr, sortMethod);

return 0;
}

void loadList(dataNode **startList, char* filename)
{
FILE *fin;
char dataA[10];
char dataB[30];
char dataC[30];
char dataD[10];
dataNode *tempPtr, *tailPtr = *startList;

fin = fopen(filename, "r");
if(fin == NULL)
{
printf("Can't open %s\n", filename);
exit(0);
}

while(fscanf(fin, "%s %s %s %s",
dataA, dataB, dataC, dataD) != EOF)
{
tempPtr = createNode(dataA, dataB, dataC, dataD);
if(tailPtr == NULL)
{
*startList = tailPtr = tempPtr;
}
else
{
tailPtr->next = tempPtr;
tailPtr = tempPtr;
}
}
}

dataNode* createNode(char* dataA, char* dataB, char* dataC, char* dataD)
{
dataNode *tempPtr;

tempPtr = (dataNode*) malloc(sizeof(dataNode));
if(tempPtr == NULL)
{
printf("Memory allocation error\n");
exit(0);
}
tempPtr->dataA = dataA;
tempPtr->dataB = dataB;
tempPtr->dataC = dataC;
tempPtr->dataD = dataD;
tempPtr->next = NULL;

return tempPtr;
}

void printList(dataNode *startList, char* sortMethod)
{
while(startList != NULL)
{
printf("%-7s %-20s %-20s %-7s\n",
startList->dataA, startList->dataB, startList->dataC, startList->dataD);
startList = startList->next;
}
}
--------------endofcode------------------
Nov 7 '06 #1
0 1575

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Francis Bell | last post by:
Hello, I'm trying to read data from a file and then insert that into a linked list. The way I have it, the program compiles, however, I'm getting a segmentation fault error message when I run...
1
by: Andrej Hocevar | last post by:
Hello, below I've attached a test program reading data from a file and storing that information in a linked list. First problem is that current->p_name will print only the last item in the file...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
4
by: scythemk | last post by:
Hi, I am writing a program that, everytime it executes, first loads all information from a file into a linked list of nodes, using struct to define it. After manipulating the data and receiving...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
8
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.