C++ heap and ifstream read function
for my assignment I am building a heap, the data for the heap is coming from a file. One of the functions is to get the data, but I am having trouble understanding the ifstream read() function and am getting quite a nasty error because of it this is what I have:
template<class T, class P>
void get_list(vector<T>& v, const char* file_loc, P func) {
T data;
ifstream inFile;
inFile.open("file_loc");
if (!inFile) {
cerr << "Error - unable to open input file\n";
exit(1);
}
inFile.read( &data, sizeof(T));
while (inFile开发者_开发知识库) {
inFile.read( &data, sizeof(T));
insert(v,data,func);
}
inFile.close();
}
the error I am receiving is:
prog7.h:53: error: no matching function for call to
‘std::basic_ifstream<char, std::char_traits<char> >::read(int*, long unsigned int)’
/usr/include/c++/4.3/istream:468: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,_Traits>::read(_CharT*, std::streamsize)
[with _CharT = char, _Traits = std::char_traits<char>]
any help would be much appreciated!
istream::read
takes a char*
. You'll need to cast: (char*)&data
.
Also, there's a bug in your code. The read can fail even after the stream test passes. The idiomatic solution is this:
while (inFile.read( &data, sizeof(T))) {
insert(v,data,func);
}
Overall, your code could be made simpler; you can open the file in the constructor and let the destructor close it. Also, I'm wondering about the first read outside the loop; do you do this with the intent to skip the first entry in the file?
template<class T, class P>
void get_list(vector<T>& v, const char* file_loc, P func) {
T data;
ifstream inFile("file_loc");
if (!inFile) {
cerr << "Error - unable to open input file\n";
exit(1);
}
inFile.read( &data, sizeof(T));
while (inFile.read( &data, sizeof(T))) {
insert(v,data,func);
}
}
The prototype for std::ifstream::read is
istream& read ( char* s, streamsize n );
So where you have
inFile.read( &data, sizeof(T));
should be
inFile.read( (char*) &data, sizeof(T));
However this assumes you are reading raw bytes from a binary file. If you are parsing a text file with the integers represented in decimal ascii characters you can just use
inFile >> data;
You instantiated the template with type int
, but there is no overloaded function for read that takes a pointer to an int as it's first parameter. You can explicitly cast to char* using reinterpret_cast
.
read()
expects a char*
(i.e. the element type of the underlying stream). So you need to cast the data pointer to char*
:
inFile.read( reinterpret_cast<char*>(&data), sizeof(T));
You have to convert the pointer to a char*
that is what the read is expecting. You have to use reinterpret_cast<char*>(&data)
in the read
. Note that this is always dangerous, as this conversion is not checked at all, and you have to be really sure that what you're reading is conforming with the T
type. The code is not portable among architectures, word sizes, etc.
精彩评论