Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can someone please help me learn about c++ file io

Status
Not open for further replies.

CosmicTruth

Technical User
Jul 24, 2006
6
US
i have the following code...
Code:
	if(!_stricmp(message, "be happy bot"))
	{
		aw_say("i am a happy bot");
		cout << "[Bot]: i am a happy bot\n";
	}

there are a zillion of these that just listen for a certain string, then reply a string and print into the console what the robot has replied.
what i would like to do (if i were a programmer) is to have a text file with all the common string and responses like...

be happy bot=i am a happy bot
hello bot=hello
bot hello=hello
bot define bot=a robot is a computer controlled entity like me

the program would open the file, look to see if the message string is in the text file line by line, if it is get the response and store it to a variable like response, then do the rest like normal:
Code:
aw_say(response); //make the bot say the response
cout << "[Bot]: " << response << "\n"; //put it on the console
can someone code this for me? ive been reading alot of books but cant grasp this, i just need a little kick in the b*%t to get me going.

im using visual c++2005 express in win XP
(me and my bot)

mnap=me not a programmer : ))
CosmicTruth
 
Include files you'll need: <fstream> & <string>

To open a file:
Code:
ifstream inFile( "path/filename.txt" );

Check if there was an error opening the file:
Code:
if ( !inFile )
{
   cout << "Error opening file!" << endl;
}

Read one line from the file:
Code:
string line;
getline( inFile, line );
You'd probably want to use getline() in a while loop to read all the lines though:
Code:
string line;

while ( getline( inFile, line ) )
{
   // Do something with the line you just read...
}

The file will close automatically when inFile goes out of scope; or you can use inFile.close() to close it yourself.

The rest is all string manipulation.
 
nope more code commented out because it would not compile : (( in a couple years i'll get the right book and figure this out : (( once i do i'll be able to reduce the program size tremendously
//int file();
//ifstream file;
//file.open ( "dialouge.txt", ios::beg );
//ifstream file( "dialouge.txt" );
////Check if there was an error opening the file:
//if ( !file )
//{
// cout << "Error opening file!" << endl;
//}
////string line;
////getline( inFile, line );
////You'd probably want to use getline() in a while loop to read all the lines though:
//string line;
//while ( getline( inFile, line ) )
//{
// if(!_stricmp(message, line))
//}

//char ch;
//char input_line[81];
//int i;
//ifstream file;
//file.open ( "dialouge.txt", ios::nocreate );
//for (i=1; file.eof(); i++){
//file.getline(input_line,80,"=");
//printf (input_line);
//file.getline(input_line,80);
//cout << input_line << endl;}
//file.close ("dialouge.txt");

if anyone else wants to uncomment some lines put it together and give up a couple minutes to get the bot reading textfiles i would be greatly thankful.

error 1 of a bunch is
'file' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

tried it with 'f' still failed
i never was good in class : ((
CosmicTruth
 
Code:
if(!_stricmp(message, line))
There's no need to use C functions like stricmp() in C++. Since you're using string variables now, they have an overloaded == operator, so (assuming message & line are both string objects) you can do this:
Code:
if ( message != line )


Code:
int file();
I'm not sure what this is supposed to be? It looks kind of like a function prototype? Whatever it is, the name will conflict with the ifstream file, so I'd delete it.


Code:
char input_line[81];
Use string instead of char arrays. Take a look at basic_string on the MSDN. You'll see all the member functions that it comes with.


Try something like this:
Code:
ifstream file( "dialouge.txt" );

if ( !file )
{
   cout << "Error opening file!" << endl;
   // exit here.
   return 1;
}

string line;

while ( getline( file, line ) )
{
   if ( line == message )
   {
      // Put your code here.
   }
}
I don't know where or how you defined message, but I'd make it a string.
 
You can save a bunch of work by using STL maps
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main ( ) {
  map<string,string> table;
  table["hello"] = "world";
  table["been"]  = "there";
  table["done"]  = "that";

  // simple lookup of a known entry
  string message = "been";
  cout << message << " maps to " << table[message] << endl;

  // test to see if an entry exists before using it
  message = "world";
  map<string,string>::iterator foo;
  foo = table.find(message);
  if ( foo != table.end() ) {
    cout << message << " maps to " << foo->second << endl;
  } else {
    cout << message << " is not in table" << endl;
  }

  return 0;
}

--
 
#include <fstream> was missing! dangit, i am not a smart guy.
Code:
	char person[255];
	char message[255];
there is some other code that loads the message variable using strcpy_s
I messed around with
Code:
if ( message == line )
and that had unexpected results even when i changed it to
Code:
if ( message != line )
the program either found that to be true to often or not at all, i think the SDK im using was originally written in C so im guessing i need to stick with
Code:
if(!_stricmp(message, "bot listen"))
!_stricmp seems to work for all the hard code text/responses i will be leaving in the bots code.
i get the file io now, im stuck still with dissassembeling the lines like "howdy bot=hello person" would be a line in the text file, i need to compair message=="howdy bot" if this is true dissassemble the line and get just "hello person" off that line and store it into a variable. I think that is what salem is telling me about but can that be done with this type string from getline file io? i was thinking more like just getline(line, 255, "=") or something....

void between_my_ears_is <(void)>
thanks for responding
CosmicTruth
 
Use the find() member of string.
Code:
map<string, string> table;

size_t pos = line.find( "=" );  // Find position of '='.

string leftStr  = line.substr( 0, pos );   // Get first part of string before '='.
string rightStr = line.substr( pos + 1 );  // Get the rest of the string after '='.

table[leftStr] = rightStr;
 
oh yes, this is good.
let me study this for a day or two and i may be able to get something going, if so i'll try and put something together and present it back here. you all are good.

ty ty ty ty
CosmicTruth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top