![]() |
|
#1
|
|||
|
|||
problem:retrieve from structThe program below encounter a problem, which when i print out a set of data retrieve from struct, it give me totally different value..the first fprintf() return me the value that i want(which are *2609_AUS74....), but when i put the fprintf() inside another for loop, it just return me [1] [1].....i have attached the input file in the attachmant...
CPP / C++ / C Code:
could anyone tell me why??and how to resolve this problem?? Any help is appreciated!! |
|
#2
|
||||
|
||||
|
Hi kelly.
The problem you are having is that you are simply assigning your char* to a static string array (sequence). Therefore all of your seq_names are simply pointing to the exact same static memory location as defined by sequence. And the contents of sequence are getting overwritten by each call to fgets. Since "[1]" is the last entry in your file, this is the apparent contents of all of your seq_names. To fix this, you need to allocate memory for you seq_names and then use the strcpy command to copy the static string contents to the new memory location. Try something like this: CPP / C++ / C Code:
Hope this helps, d |
|
#3
|
|||
|
|||
|
thanks dsmith,
yup, its really solve my problem CPP / C++ / C Code:
to CPP / C++ / C Code:
but the program also cannot return me the correct answer in the 2nd fprintf..(it also return only [1][1]..) |
|
#4
|
||||
|
||||
|
Hey kelly. I am not sure that I understand your subscripting. I may be wrong, but shouldn't this line:
CPP / C++ / C Code:
Be more like: CPP / C++ / C Code:
Otherwise you are just printing the last element each time. HTH, d |
|
#5
|
|||
|
|||
|
thanks dsmith,
actually i also just realise that..see how careless i am.. ![]() |
|
#6
|
|||
|
|||
|
how to free the memory for these:
CPP / C++ / C Code:
thanks.. |
|
#7
|
||||
|
||||
|
Quote:
The opposite of malloc is the free command. So you would use: CPP / C++ / C Code:
Cheers, d |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Urgent ! Pls Help Me ! | mycashmoney | C Programming Language | 4 | 01-Jul-2006 23:49 |
| Project | amir_b | C Programming Language | 18 | 04-May-2004 23:40 |
| reading a char* into struct data | spike666 | C Programming Language | 7 | 19-Apr-2004 13:06 |
| Help with struct IORB | cIdiot | C Programming Language | 1 | 19-Apr-2004 09:09 |
| struct | fj8283888 | C Programming Language | 2 | 15-Apr-2004 13:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The