B
Blastbeat
Hello all,
I'm having a hard time extracting elements from a datafile into a STL
vector. The contents of the datafile are as follows:
element1
element2
element3
....
My goal is to read in each element line by line & store each line into
a vector. I want to use a vector because my datafiles will vary in
size. I'm fairly certain that memory is getting over-written in the
while(!OpenAnimations.eof()) loop. I've tried using an sprintf
statement to overcome this, but to no avail. I'm stuck, please help!
I'm using MSVC++ 6.0.
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <fstream>
char read_animation[101];
char vector_data[101];
int main(int argc, char* argv [])
{
// Define Vector
vector <char*> animation_names;
ifstream OpenAnimations("file.csv", ios::in);
if(!OpenAnimations)
{
cerr << "The file failed to open!\n";
exit(1);
}
// Open & Insert animation names into data structure
while(!OpenAnimations.eof())
{
OpenAnimations.getline(read_animation, 100);
sprintf(vector_data, "%s", read_animation);
animation_names.push_back(vector_data);
}
// Search for a string in elements of the vector
start = animation_names.begin();
end = animation_names.end();
for(start; start!=end; start++)
{
if ( strstr(*start, "string"))
{
cout << "Executing string match!\n";
function_to_do_something(*start);
}
}
OpenAnimations.close();
return 0;
}
I'm having a hard time extracting elements from a datafile into a STL
vector. The contents of the datafile are as follows:
element1
element2
element3
....
My goal is to read in each element line by line & store each line into
a vector. I want to use a vector because my datafiles will vary in
size. I'm fairly certain that memory is getting over-written in the
while(!OpenAnimations.eof()) loop. I've tried using an sprintf
statement to overcome this, but to no avail. I'm stuck, please help!
I'm using MSVC++ 6.0.
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <fstream>
char read_animation[101];
char vector_data[101];
int main(int argc, char* argv [])
{
// Define Vector
vector <char*> animation_names;
ifstream OpenAnimations("file.csv", ios::in);
if(!OpenAnimations)
{
cerr << "The file failed to open!\n";
exit(1);
}
// Open & Insert animation names into data structure
while(!OpenAnimations.eof())
{
OpenAnimations.getline(read_animation, 100);
sprintf(vector_data, "%s", read_animation);
animation_names.push_back(vector_data);
}
// Search for a string in elements of the vector
start = animation_names.begin();
end = animation_names.end();
for(start; start!=end; start++)
{
if ( strstr(*start, "string"))
{
cout << "Executing string match!\n";
function_to_do_something(*start);
}
}
OpenAnimations.close();
return 0;
}