Hey guys, i am wanting to search for a particular striing inside of an ordiinary text file.
I could not find anway to do this except using getline to take in the file line by line and then use the string find function to find the string in question. The problem that i am having is when i put this process into a loop, the program fails. As in the loop no longer appears to want to exit at all.
The code snippet in question is here:
What happens is if i enter in the filename correctly first time, it appears to work but if i dont enter it in correctly first time, the loop will not exit even if you type it in corectly the second time.
Being now curious i then tried the code below, which doesnt work at all!
I have spent loads of time trawling the net which makes various references to clearing the input buffer using cin.ignore() and cin.clear() but nothing i try works here. ARRGGGGHHHH!!
Edit: it may not be clear but file_name is a private data member of type string. I initially tried using file.open(file_name.c_str(), ios::in | ios:
ut); but the same problem occurred
Also, file is an fstream object
thanks ;-)
I could not find anway to do this except using getline to take in the file line by line and then use the string find function to find the string in question. The problem that i am having is when i put this process into a loop, the program fails. As in the loop no longer appears to want to exit at all.
The code snippet in question is here:
Rich (BB code):
database::database()
{
cout << "Please enter in the name of the database file.\n";
char Char[200];
do
{
cin >> file_name;
cout << "the contents of file_name is now:" << file_name <<"test" << endl;
strcpy(Char,file_name.c_str());
file.open(Char, ios::in | ios::out); //open file for both input and output operations. Need to change to null terminated character string
if(file.fail())
cout << "Error opening file\nPlease recheck file name and re-enter.\n";
}
while (file.fail());
}
Being now curious i then tried the code below, which doesnt work at all!
Rich (BB code):
#include <iostream>
using namespace std;
void main()
{
char Char[200];
do
{
cout << "enter string.\n";
cin >> Char;
cout << Char;
}
while (Char != "hello");
}
Edit: it may not be clear but file_name is a private data member of type string. I initially tried using file.open(file_name.c_str(), ios::in | ios:
Also, file is an fstream object
thanks ;-)