Creating Linked Lists from items in a file
Hi all. I am very rusty on my C programming (haven't done it in many many years). I am trying to create a linked list and populate it with data from a file. I am checking to see if a file is present, and if it is, load the data from the file into a linked list of data elements. Here is the "struct" of the linked list:
Code:
typedef struct Here is the relevant part of the program: Code:
void add(ssidlist **head, char * ssid) { Mark |
There are a couple of problems that jump out right away. First, you have declarations like this:
Code:
ssidlist ssids = NULL; Code:
ssidlist *ssids = NULL; Second, when you call add(), you use: Code:
add(*ssids, x); Code:
add(&ssids, x); Lastly, your string handling does a couple of bad things. First: Code:
x[strlen(x)] = '\0'; Also: Code:
tmp->ssid = ssid; There may be other issues, but those are the ones that jumped out at me. |
Re: Creating Linked Lists from items in a file
I changed a few things I saw:
Code:
void add(ssidlist **head, char * ssid) { |
edit: spooon beat me to it, use his instead ;)
I think you should process the file, one line at a time using fgets(3) or a similiar line processing function. This is the way I would probably approach the problem. I'm not sure which version of getline(...) you are calling, is it the one from C++ <iostream>?? Or some function you defined? Because it doesn't look like you allocate any space for your char* pointer. This would obviously be a critical error but I would need to know more about your code. Anyways, the general path I would follow would look like this mixture of C/pseudocode: Code:
#define MAXLINE 1000 |
Thanks for the help. I'll try that as soon as I get a chance. One question, though. What does the '&' do when you put it before a variable?
Mark |
It is the "address of" operator. Think of it as the reverse of the dereference operator.
The & applied to a standard data type (say an int) returns a pointer to that int. Similarly, if you use the & on a pointer, you get a pointer to a pointer. That's what your add() function requires: ssids ** => a pointer to a pointer to a struct. |
All times are GMT -5. The time now is 12:11 AM. |