.txt elements into an array
hi, I have a .txt file something like this:
comics
shona
91765
0
0
China
name
97245
0
0
|
how can I input the first five elements in .txt file into an array, then the next and so on?
array size = 5???
then allocate new array after 5th element for the next (5-10) .txt input ???
Thank you.
this kind of what you mean?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
for(int x=0; x<2;x++)
{
for(int y=0;y<5;y++)
{
if(x==0)
{
infile>>array1[y];
}
if(x==1)
{
infile>>array2[y];
}
}
}
|
or are you talking about dynamically creating a new array every time a counter reaches five?
Thank you.
dynamically creating a new array every time counter reaches five.
like this?:
count first 5 elements put it into Book array
count next 5 elements put into (new?) Book array
Book is a object.
Any suggestions?
You could use a vector of strings to automatically dynamically allocate memory.
1 2 3 4 5 6 7 8
|
std::vector<std::string> word;
std::ifstream fin("input.txt");
while(fin.good())
{
std::string temp;
getline(cin,temp);
word.push_back(temp);
}
|
Last edited on
Topic archived. No new replies allowed.