473,558 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please help solve these linked list and sscanf problems

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 for each
item. Where is the problem? Is it in the sscanf? Whay can't I abandon
tmp_name and just use p_name instead in that sscanf? Second, the
order of the output (with a file of 3 lines) is 1, 2, 0, why? And
why is the item 0 empty? I'm new to linked lists and will be
thankful for clarifications.

I really appreaciate your help, thanks
andrej

--
echo ${girl_name} > /etc/dumpdates

=============== =============== =============== =============== ========

#include <stdio.h>
#include <fcntl.h>

int main ()
{
struct conf
{
char *p_name;
char p_up;
char p_down;
int x;
struct conf *next;
};

FILE *conf_fd;
struct conf *head = NULL;
struct conf *current = NULL;
struct conf *new = NULL;

char *tmp_name, tmp_up, tmp_down;

char buf[BUFSIZ];
int i;

conf_fd = fopen("/tmp/conf", "r");

for (i = 0; fgets(buf, BUFSIZ, conf_fd) != NULL; i++)
{
if (*buf == '\n' || *buf == '#')
{
i--;
continue;
}

sscanf(buf, "%s %c %c ", tmp_name, &tmp_up, &tmp_down);

if (head == NULL)
{
new = (struct conf*) malloc(sizeof(s truct conf));
new->next = head;
head = new;
new->p_name = tmp_name;
new->x = i;
}
else
{
current = head;
while (current->next != NULL)
current = current->next;

new = (struct conf*) malloc(sizeof(s truct conf));
current->next = new;
new->next = NULL;
current->x = i;
current->p_name = tmp_name;
}
}

current = head;
while (current != NULL)
{
printf("%i %s %c\n", current->x, current->p_name, current->p_up);
current = current->next;
}
}

Nov 13 '05 #1
1 3035
On Mon, 18 Aug 2003 11:23:13 GMT, Andrej Hocevar <dr******@volja .net>
wrote:
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 for each
item. Where is the problem? Is it in the sscanf? Whay can't I abandon
tmp_name and just use p_name instead in that sscanf? Second, the
order of the output (with a file of 3 lines) is 1, 2, 0, why? And
why is the item 0 empty? I'm new to linked lists and will be
thankful for clarifications.

I really appreaciate your help, thanks
andrej

--
echo ${girl_name} > /etc/dumpdates

============== =============== =============== =============== =========

#include <stdio.h>
#include <fcntl.h>
You need to include <stdlib.h> for malloc(). Also <fcntl.h>
is never used.
int main ()
{
struct conf
{
char *p_name;
char p_up;
char p_down;
int x;
struct conf *next;
};

FILE *conf_fd;
struct conf *head = NULL;
struct conf *current = NULL;
struct conf *new = NULL;

char *tmp_name, tmp_up, tmp_down;

char buf[BUFSIZ];
int i;

conf_fd = fopen("/tmp/conf", "r");
There should be a return code check in case the file does not exist.
for (i = 0; fgets(buf, BUFSIZ, conf_fd) != NULL; i++)
{
if (*buf == '\n' || *buf == '#')
{
i--;
continue;
}

sscanf(buf, "%s %c %c ", tmp_name, &tmp_up, &tmp_down);
You should also check the return code here.

This is also the first signficant error; tmp_name is just a pointer.
It isn't yet initialised to point anywhere. Also if this just
''happens to work'' for the first line of the file it won't for the
second line which will overwrite the string retreived by the first
line.
if (head == NULL)
{
new = (struct conf*) malloc(sizeof(s truct conf));
You don't need the cast. You probably added it because the compiler
gave a warning about missing #include <stdlib.h>, but this is the
wrong way to suppress the warning.

Also you should check that new != NULL.
new->next = head;
head = new;
new->p_name = tmp_name;
This assumes that you are going to call malloc() to allocate a
different value for tmp_name each time around the loop.
new->x = i;
}
else
{
current = head;
while (current->next != NULL)
current = current->next;

new = (struct conf*) malloc(sizeof(s truct conf));
current->next = new;
new->next = NULL;
current->x = i;
current->p_name = tmp_name;
These two lines are also wrong. current is pointing to the last
item in the list. these should assign new->x and new->p_name.
}
}

current = head;
while (current != NULL)
{
printf("%i %s %c\n", current->x, current->p_name, current->p_up);
current = current->next;
}
return 0;}


Nick.

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2809
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I enter a number, can I use to call a particular node ? eg enter a number: 3 calling node of number 3 is it possible ?
6
4588
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any potential pitfalls I'd be extremely grateful. Cheers Steve
4
4227
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
6
2423
by: Rylios | last post by:
I am trying to make a very basic text editor using a linked list, fstream, sorting... This is what i have so far ------------------------------------------------------------------------------------------------------------------------------------ The Linked.H --- Header file #include <iostream> using namespace std;
33
2838
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio...
22
8015
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive solution. Being that recursion is frowned upon in embedded software, the answer was not what the interviewer expected, but alas it was correct. I...
6
4146
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 try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output,...
6
2180
by: shapper | last post by:
Hello, I am creating a form that includes a few JQuery scripts and TinyMCE Editor: http://www.27lamps.com/Beta/Form/Form.html I am having a few problems with my CSS: 1. Restyling the Select
6
4723
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following format: struct mst_ {
0
7549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7835
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7593
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6183
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5455
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.