initialize array of unkown size?

is this possible in cpp?

if i want to declare an array of undetermined size, my intuition would tell me that i could use something like

int array[]


but if i try this i get a "storage size is not known" problem


this is leading to my question that if i am reading an input file that i will not know the size of, how do i work around this?

is there a workaround, like counting the number of lines in the input file first?
Specifically, vector would be my first choice without having more details of your requirements. Vectors are guaranteed to use contiguous blocks of memory as do the basic arrays and so vectors can be used anywhere that a regular C style array can be used. If you are just looking for a basic c style array that is dynamic in size, I would start reading the sections on the vector first.
i just tried that, still kind of confused though...

say i had an input file that looked either looked like

A B C D
E F G H

or

A B C D
E F G H
I J K L

and my goal was to create four vectors (one per column) so i could have either {AE} or {AEI} , and so on.

i just want to store the data (which could be any numbers of rows) into separate column vectors when i read it, given the fact that i know the texture of the data (# of columns) but not the sample size of data (rows)
Last edited on
First, you need to research file streams. do you know how to use file streams to actually read the data in? You can use streams to read the file one line at a time into a std::string. In a for loop you can copy the character for each std::string into a different vector until you have processed all of the characters. Then read the next line and continue until EOF is reached. Since there is no good way to know how many lines are in the file you'd probably just use the vector::push_back to insert the characters after each read operation.
still learning about reading the data in cpp, i will admit i am sort of new to this lang, but i learned programming off higher level langs where array operations and file reading were more intuitive and robust than in cpp, so i am trying to break the mold of thinking ....

to read data in my fashion i am pretty sure i need a while loop.

for instance, if i declare the following

ifstream infile("data.txt")

string firstrow[3];
string secondrow[3];
string thirdrow[3];
string lastrow[3];

while (infile)

infile >> firstrow[i] >> secondrow[i] >>thirdrow[i] >> lastrow[i];
i++;

i can get it to write the input data into sub-arrays, but the problem is i declared them with a size in mind beforehannd

i tried using vectors for this with the push_back command, but i got errors :(
Using a while loop is a good start but don't try to read every single line at once. You only need to read one line at a time. Then you might want an embedded for loop to take each character out of the string.

This is really a rather bizarre kind of a problem because you don't know what the widest column is until you have reach each line. Trying to make a vector for each column might be tricky. You don't really know how many you need until you read a line and discover new columns. What if each line has totally different numbers of characters (unless the program expects a certain file format and handles exceptions if the file isn't formatted correctly).

Do you have a particular reason for this set of requirements or are you just looking for a way to make up a problem in order to teach yourself the language?
http://www.cplusplus.com/doc/tutorial/files.html contains some useful tidbits and an example on how to read a file one line at a time into a string.
first, thanks2all for the suggestions so far.

this is both for a set of requirements and me teaching myself the language.

i guess i really dont understand why i can declare an empty array, then fill it with data. i have been reading about vectors, but i dont understand why i cant read data from a file into a vector.

for the problem, i actually will known the format of the data expected, whihch will be

string int string it

for example, an input file named 'petstore inventory' might be

cats 1 dogs 12
cats 8 fish 123
dogs 0 gerbils 2

or it might be 10 more rows of this...

so in other languages, it would be really easy to write this data into a matrix, and format the columns as strings or nums, then perform row or column operations as needed

conceptually i cant do this in cpp... it seems like vectors are how you could do it, but i cant get it to work.

ideally, i want to read the input file line by line, but parse the data into four separate columns.

let me ask you - would you do it this way, or try to use classes or structs for parsing the data?

Topic archived. No new replies allowed.