![]() |
Reading from file and adding values to array
Reading from file and adding values to array
-------------------------------------------------------------------------------- I am new to the C/C++ My Program: Code:
int main(int argc, _TCHAR* argv[])10 20 30 40 My Question: I am able to read the data from the file from the above loop of reading the file but when i add it to the array the value incrementing the array and assiging it, the values gets changed first time from the txt file i get 10 and i add to CheckPoint[i] = datatext; where initially i =0, the next loop when the value for the datatext = 20 the initial value get to 20 and also inserts 20 in the second place Where am i going wrong Please help me |
Re: Reading from file and adding values to array
I don't see the i variable being declared as well as incremented and so I think your data is getting changed.
|
Re: Reading from file and adding values to array
> CheckPoint[i] = datatext;
This is only a pointer, so when datatext is changed next time around the loop, then all the pointers to it will also appear to change as well. Make CheckPoint a true 2D array of chars, and use strcpy() to copy datatext. Or you might consider http://www.hmug.org/man/3/strdup.php, but that would not be a portable solution (although strdup is very easy to write yourself). |
Re: Reading from file and adding values to array
Thank You Shabbir & Salem
Sorry to post a wrong code for not giving the initilizing the i variable as i have declared in my program strdup worked out Thank you once again |
| All times are GMT +5.5. The time now is 12:39. |