Sorry for the kind of useless topic, but I'd like someone more experienced to tell me if I go the right way about it.
I'm trying to parse very simple commands from an external text file using a vector of char[]'s and std::ifstream infile, but it seems a little clunky to me, so I have doubts about it.
In this test program, I'm trying to get the function to recognize and react to new lines and a smiley face.
This is the text file:
data.txt
Hello, I am a text file! SMILEY And this is a test!
SMILEY
SMILEY A very happy test! SMILEY
...And this is the program I test the method with:
main.cpp
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
struct character { char ch[10]; };
int main(int argc , char * argv[] )
{
std::vector<character> word_list;
character ch_main;
std::ifstream infile( "data.txt" );
while( infile >> ch_main.ch )
{
word_list.push_back( ch_main );
if( infile.peek() == '\n' )
{
character ch_temp;
strcpy( ch_temp.ch , "LINE" );
word_list.push_back( ch_temp );
}
}
for( int i = 0 ; i < word_list.size() ; i++ )
{
if( !strcmp( word_list[i].ch , "LINE" ) ) std::cout << std::endl;
else if( !strcmp( word_list[i].ch , "SMILEY" ) ) std::cout << ":-) ";
else
{
std::cout << word_list[i].ch;
std::cout << " ";
}
}
std::cin.ignore();
return 0;
}
My intention is to find a simple method to read external files of component lists for a component based entity system. I have in mind something like this:
enemy behavior.txt
walk : looking_direction , speed 10 , seconds 2
shoot : bullet.txt , looking_direction , times 3
turn_around
Bear in mind I have zero experience with component systems, so I might go completely wrong about the whole thing.
Thank you in advance. :-)