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;
}
} 1 3041
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
|
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
|
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
|
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;
|
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 2005. Everything is standard C (ANSI C ?? I don't
know the difference) - but since so many functions...
| |
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 asked
some friends how they would have answered and another answer is to
reverse the list and then...
|
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, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
|
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
|
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_
{
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |