>> is an operator, getline is a function so their syntax is quite different
So if you have the stream file and the string temp, You should call it this way: getline ( file, temp );
ifstream* openFile(istream& input)
{
for(;;)
{
// open the file specified by the user
char fileName[80];
cout << “Enter the name of a file” << endl;
// read input from the user in such a way
// that the input can’t overflow the buffer
input.getline(fileName, 80);
//open file for reading; don’t create the file
//if it isn’t there
ifstream* pFileStream = new ifstream(fileName);
if (pFileStream->good())
{
return pFileStream;
}
cerr << “Couldn’t find “ << fileName << endl;
}
return 0;
}
How can I include this to my current codings to replace the ifstream file function?
Get a string as usual ( with getline would be better than with >> ) before the declaration of 'file' ( the declaration are better if scoped inside main ).
Then pass as argument of file's constructor thatstring.c_str() instead of "knowing.txt"
eg:
1 2 3
string filename;
getline ( cin, filename ); // get input
ifstream ( filename.c_str() ); // open file, c_str() converts the std::string into a C string ( character array )
Thanks soo much for the advice given. The temp2 in the code was a typo on my part. Sorry about that.
As for the class, ur example is howing me how to declare it using stream but how do i do it when i'm using getline instead? Furthermore, is there any differences if i would want to make a header class, whereby the the execution class cointain the main codes?
As I explained to flubber there's just some syntax difference between using >> and getline
You can declare the class functions on a header and implement their bodies in a source file as long you link all cpp files ( this is usually done automatically if you are using an IDE )
Notice that the declarations on lines 9-10 are wrong ( void string ) a function can only return 1 type
A good practice is avoiding using directives in headers and adding header guards:
#include "matchPattern.h"
int main (int nNumberofArgs, char* pszArgs[])
{
cout << "Enter Movie File:";
getline ( cin, filename );
ifstream file ( filename.c_str() );
while ( file.good() )
{
file >> temp;
if ( temp == keyword_runtime ) // check the first keyword word
{
//How can I call my class to output my getline and how to inform the string that my keyword is "Runtime:"
}
if ( temp == keyword_director ) // check the first keyword word
{
//How can I call my class to output my getline and how to inform the string that my keyword is "Director:"
}
Notice:
- string keyword_title(); & similar are declaring functions
- You cannot call code from the class body, you need to put it in a member function.
(simplified) example of what I think you should have:
// Headerclass matchPattern { // the name of the class seems that you shouldn't look for specific keywords in the file but they should be given by the user, anyway I'll just show you how to get the 'Director' stuff
public: // everything is public to be simpler
std::string director, keyw_dir; // member variables
friend istream & operator >> ( istream &, matchPattern & ); // already explained in previous posts
friend ostream & operator << ( ostream &, const matchPattern & ); // already explained in previous posts
matchPattern ( ) : keyw_dir ( "Director:" ) {} // the constructor is so small you can leave it in the header or move in the source file if you prefer
};
// Forward declaring the friend operators
istream & operator >> ( istream &, matchPattern & );
ostream & operator << ( ostream &, const matchPattern & );
___________________________________________________________________
// Source
istream & operator >> ( istream &is, matchPattern &mp )
{
string temp; // local variable
while ( is.good() )
{
// everything is as before just 'file' is called 'is' (from the function parameter) and you have 'mp.' before the class members
is >> temp;
if ( temp == mp.keyw_dir )
getline ( is, mp.director );
}
}
ostream &operator << ( ostream &os, const matchPattern &mp )
{
// as you were doing with cout
os << "The director is " << mp.director;
}
___________________________________________________________________
// Main sourceint main()
{
matchPattern myMatchPattern; // declare a class object
string filename;
// get filename...
ifstream file ( filename.c_str() );
file >> myMatchPattern; // here you are using your own overload of the >> operator
cout << myMatchPattern; // << operator
}
Yes, wanthor and I are 2 different people. But we are frends. I ask him to join this forum because I told him that I have found alot of tips and guidance from you.
Tulang also our friends. We are all C++ beginners.
I have a situation here and need your advice. I have found out that when I used a getline function, the extracted value that was retrieved have extra characters inside.
For example, Director: Alex Tyson
If I used the getline, the temp will show a value of "Alex Tyson//r" and not "Alex Tyson" purely.
Can you advice how can i remove the hidden //r character from the string?
I need to remove this as I need to do a test stating that temp == "Alex Tyson" is true.
But as they still detect as "Alex Tyson//r" == "Alex Tyson", it always shows as false.
1 2 3 4 5 6
file >> temp;
if ( temp == keyword_director ) // check the first keyword word
{
getline(file,temp);
cout << keyword_runtime + temp + '\n' + '\n';
That's the Microsoft end of line encoding. Instead of having just a \n it has both \r and \n
so use getline( stream, string, '\r' ); and discard the following \n character
yourgetline pseudocode ( parameters = istream&, string& )
declare a charwhile the char != \r or \n
istream.get the char
append the char to the string
if the char == \r
peek next charifchar == \n
remove it from the string