Read value from a file and store them in array

I have this text file which contains values of 1s and 0s only. I need to separate the 1s and 0s and store them in an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
// My initial solution

file3.open( "read.txt", fstream::out | fstream::in );

int Ones[];// all ones here 
int Zeros[];// and all zeros here

while( !file3.eof() )
{
  //if value from file is 1 store it in Ones[]
  //else store it in Zeros[]
}


The problem is I don't know what size to give to the array since I'm reading from the file.

Any suggestions?

closed account (j2NvC542)
If you don't know what size you want the array to be, use a vector instead. It is dynamic and you can push_back() values easily into it.

http://www.cplusplus.com/reference/stl/vector/
thanks I already got it :)
Hi,
projectuser could you please tell me how you did it?
i also want to extract some values from a file and then use only a value at
a specific position.
Here is what i did so far:

char str[] ="E0210|05|87785781|000000000000|0|5359245001069019|Do not honour|||1106|20||20120806092539|710|||MASTER CARD HOLDER /||||||"; //this is what i get from the file
char * pch;
int i;

pch = strtok (str,"|");
i=1;// im using this so that it controls the number of time the loop iterates

while (pch != NULL)
{

pch = strtok (NULL, "|");
i++;
if(i==7)//i break the loop here because i actually need this field
//it is a message that i return to the User and this message will always be between the 6th and the 7th | symbol
//ist there a way to specify it directly? please note that i cannot use the length or the position because it is not fix.
{
Application->MessageBoxA(pch,NULL,NULL) ;
break;
}
}
The problem with the code above is that the loop does not even get in my if statement sometimes it just exits.
Thank you
1
2
3
4
5
6
7
8
9
vector<int> zero;
int num

while( file3 >> num )
{
  if(num == 1)
    zero.push_back(num);
}
Topic archived. No new replies allowed.