I have an text file which contain integers value.
I have made the code to read the file but i am not able to dynamically allocate array and store the value in the array.The file may sometime contain 50 value and sometime contain 300 value.
The file may also contain the value that are repeating integers for example the file may be as follows:- 1,2,3,1,1,1,1,2,2,2,21,5,5,5,5,5,5,4,5,45,4,54,544,
I want the value to be stored in array only once and allocate array at runtime.Please help me to do this by telling sample code.
This is actually a very simple problem and you should be able to work it out for yourself. How can you examine the file to discover how many values are in it? You could open it and start reading the values, and when there are no more values to read, you know how many there are in that file.
An alternative is to just use a proper C++ container that takes care of its own size.
> The file may also contain the value that are repeating integers for example the file may be as follows:-
> 1,2,3,1,1,1,1,2,2,2,21,5,5,5,5,5,5,4,5,45,4,54,544,
> I want the value to be stored in array only once and allocate array at runtime.
Is there a reason for not using the standard C++ library?
@Tarun Batra
Do you know the range of the values that is the minimum and maximum values that can be stored in the file? For example 0-500 or 0-1000?
If you know apriori the range of values you can use std::bitset.:)