-
Starting with c++
Hi,
I am just starting with c++ and i am a bit confused. My "a priori" knowledge is c and in c if i have a large file that i wish to read in into an array, having no clue how large is the file i can allocate an array of size X and the if X is exceeded a reallocate it by, let say, Y. now how is this resolved in c++. I am asking this because i see that c++ uses new() and delete() for memory allocation ? But how is the memory reallocation done?
Thank you
Baxy
-
You would avoid it by using one of the standard containers, like vector, instead of an array.
-
is it possible to get some example ? because if i have a file like this:
Code:
1
2
3
4
5
6
67
7
2
12
3
and i need the numbers in my array. do i then first store them into container and the restore them into array ??
thnx
baxy
-
You would use the container instead of the array.
Jim
-
At its most basic, the outline you'd want is the following:
- Create an ifstream object for your input file. Open the file.
- Create a vector<int> object for storing your integers that you've read from the file.
- Create a single temporary int to read into from the file.
- While you can successfully read an integer from the file into the temporary int do the following:
- Push the temporary int's value onto the vector
- Do stuff with the ints now stored in the vector
Relevant headers for the above would be at least <fstream> and <vector>, probably more (<iostream>) for user I/O.
Do a quick bit of research and post your attempt if you are having problems.