copy strings problem

This is a discussion on copy strings problem within the C++ Programming forums, part of the General Programming Boards category; a have a struct like this : struct nodo_text { int entero1; char* texto; nodo_text* SIGUIENTE; }; i have a ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    copy strings problem

    a have a struct like this :

    struct nodo_text
    {
    int entero1;
    char* texto;
    nodo_text* SIGUIENTE;
    };

    i have a simple text file, which i read with fgets to get the text, like this :

    nodo_text* nodo_aux;
    char line[255];

    while (fgets(line, MAX_TEXT, hSource) != NULL)

    up to here everything is ok. But because i want to implement a simple linked list, i do this too :

    nodo_aux = new nodo_text;
    nodo_aux->texto = line; /* HERE IS THE PROBLEM */

    if i leave it like that, the last line that comes from the file i am reading stays in all the nodes of the list. I jsut want to copy from line to my text member node list!!!!

    a cannot find a way to copy from line (an array) to
    nodo_aux->texto. if i use strcpy like this :

    strcpy(nodo_aux->texto, line);

    i get an access violation error.

    If i declare line to be :

    char* line;

    i get an assertion failed.

    does anybody can help me???

    thanks a lot in advance,

    __Dgc

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Simple way:

    change this:
    > char* texto;
    to this
    > char texto[255];
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    but then i have a limited text width of 255, which is something i dont want, cause i am getting data from a stream, and this could be larger than 255 at the first line.

    how can i copy from line[255] to char* texto????

    thanks for your reply.

    __Dgc

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,664
    You can't, because 'texto' doesn't have any memory associated with it.

    >> nodo_aux->texto = line; /* HERE IS THE PROBLEM */

    That line of code just forms a pointer to the array 'line', and since all of your pointers are pointing to the same buffer, whatever you last copied into it is what prints from any of those pointers. Either make it a buffer of predetermined size (as Hammer suggested), or else dynamically allocate the memory. You might consider using an std::string, too.



    ITSA
    Socket Library!

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    thank you. i used

    nodo_Text-> = new char[MAX_TEXT];

    it worked fine.

    thank you very much.

    __Dgc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Critical problem dealing with a copy and pasted version
    By DivingPhoenix in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2007, 02:41 AM
  2. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  3. Problem with else on strings
    By [Z-D] in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 09:41 PM
  4. Problem with strings
    By yurisk1 in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 08:32 AM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21