getline and linked list problem

From: Kay (ericjo92003_at_yahoo.com.hk)
Date: 08/30/04


Date: Mon, 30 Aug 2004 22:33:03 +0800

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);
    }



Relevant Pages

  • Re: getline and linked list problem
    ... THX for your reading. ... Kay wrote: ... > This function is used getline function to get data. ... However, the strcpy that I ...
    (comp.lang.cpp)
  • Re: Secure C programming
    ... So take the case of the string functions. ... my code uses strcpy to stuff it into a buffer. ... but efficiency in processing them. ...
    (comp.lang.c)
  • Re: Two Questions about "strlen", "strcat" and "strcpy"
    ... The reasoning is that unsigned values have a larger maximum value and ... incremental string concatenation is simplified ... fact calls to strlen, implicit or not, is the real performance ... People may write strcpy() in "hand coded assembly language" if they ...
    (comp.lang.c)
  • Re: when to use strncpy() ?
    ... >> It works because strncpy() returns the address of the dest argument. ... If the user runs this with parameter "1234567890", the buffer will ... If the strcpy were replaced by ... strcpyis completely safe to use /if/ you know that the string will ...
    (comp.lang.c)
  • Re: strcpy() - dangerous? [Was Re: gets() - dangerous?]
    ... strncpy is safer than strcpy and if I try to use ... /* str_size + 1 is based on strlen not including the terminating null in the size of the string. ... Why don't you use memmove which *does* exist because it is part of the C standard? ...
    (comp.lang.c)