473,396 Members | 2,085 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,396 software developers and data experts.

getline and linked list problem

Kay
This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?
void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, '\n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one

char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}

Jul 22 '05 #1
2 2858
Kay wrote:

This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?

void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, '\n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one

That should be ok.
It might be that you managed to blow up the memory management
in a previous call or somewhere else in your program ...
char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);
.... this is suspect.
namet points to memory with the sizeof a char pointer.
Yet you insist on copying an arbitrary number of characters
to it. Shouldn't the above sequence read

namet = (char*)malloc( strlen( name ) + 1 );

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}


PS: Is there a reason you are using malloc in a C++ program?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Kay
Yes, I can solve this problem. THX for your reading. ^^

Kay wrote:
This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?
void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, '\n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one

char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}


Jul 22 '05 #3

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

Similar topics

10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
2
by: Peter Gordon | last post by:
Is using getline with a fstream reference covered in the standard? I have three C++ compilers on my system. Two of them compile the code below without warnings. The third returns the following...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
11
by: bofh1234 | last post by:
Hello, I am having a problem with linked lists. My program is based on a client server model. The client sends some packets of data to the server. The server reads those packets and is...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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
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,...

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.