Reputation: 10137
Is this the proper way to open a file for input?
void BinaryTree::read(char * path, int line_number)
{
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization
file->seekg(0, std::ios::beg);
int length = file.tellg();
char * buffer = new char[length];
file->getline(buffer, line_number);
printf("%d", length);
file->close();
}
I'm guessing not, because the compiler won't accept a char
array, or a std::string
for the ifstream
constructor, yet when I read documentation, I see string
s and/or char
arrays being passed to ifstream
constructors.
Is something wrong with my compiler or am I just using the wrong type in my parameter?
Upvotes: 0
Views: 298
Reputation: 264431
Couple of things I would change:
void BinaryTree::read(char * path, int line_number)
{
// Use an object not a pointer.
ifstream* file(path);
// When you open it by default it is at the beginning.
// So we can remove it.
file->seekg(0, std::ios::beg);
// Doing manually memory line management.
// Is going to make things harder. Use the std::string
int length = file.tellg();
char * buffer = new char[length];
file.getline(buffer, line_number);
// Printing the line use the C++ streams.
printf("%d", length);
// DO NOT manually close() the file.
// When the object goes out of scope it will be closed automatically
// http://codereview.stackexchange.com/q/540/507
file->close();
} // file closed here automatically by the iostream::close()
Simplified here:
void BinaryTree::read(char * path, int line_number)
{
ifstream file(path);
std::string line;
std::getline(file, line);
std::cout << line.size() << "\n";
}
Upvotes: 0
Reputation: 2050
You've got more problems than the ones mentioned above though:
apologies if I've missed the point here
Upvotes: 0
Reputation: 27201
No pointer needed, as @Nawaz said:
ifstream *file(path);
Potential memory leak:
char *buffer = new char[length];
You should delete[]
it afterwards:
delete[] buffer;
...But, it's a lot easier to use std::string
:
std::string buffer;
Final code:
std::string BinaryTree::read(std::string path, int line_number)
{
std::string buf;
ifstream file(path.c_str());
if(file.is_open())
{
// file.seekg(0, std::ios::beg); // @Tux-D says it's unnecessary.
file.getline(buf, line_number);
file.close();
}
return buf;
}
Upvotes: 0
Reputation: 12212
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization
The problem is that the construction of the object is not appropriate. You're probably trying to do the following (or something similar), indeed passing a char array to the constructor of the ifstream object:
ifstream file(path);
However, the introduction of an asterisk here changes the whole meaning. You're creating a pointer to an object ifstream
, but not the object ifstream
itself. And in order to construct a pointer, you would need another pointer to an ifstream object (i.e. a pointer of the same type).
ifstream file(path);
ifstream * ptr( &path );
This is not what you intended to do, anyway, you probably wanted to create an ifstream
object referenced by a pointer:
ifstream * file = new ifstream( path );
//... more things...
file->close();
But remember that the object must free'd when it is not needed anymore. Objects referenced by pointers are not automatically free'd as it happens with normal (objects in the stack) objects.
ifstream * file = new ifstream( path );
//... more things...
file->close();
delete file;
Hope this helps.
Upvotes: 1
Reputation: 361472
Dont use pointer. It is not needed here.
Try this:
ifstream file(path);
and then use it as:
//...
file.getline(buffer, line_number);//file->getline(buffer, line_number);
//...
Upvotes: 2