copy strings problem
a have a struct like this :
i have a simple text file, which i read with fgets to get the text, like this :
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 :
i get an access violation error.
If i declare line to be :
i get an assertion failed.
does anybody can help me???
thanks a lot in advance,
> char* texto;
> char texto;
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 to char* texto????
thanks for your reply.
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.
thank you. i used
nodo_Text-> = new char[MAX_TEXT];
it worked fine.
thank you very much.