ifstream operator>>
-
Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)
-
Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)
char idata[256]; ifstream a_file ( "data/text.dat" ); while (! a_file.eof() ) { a_file.getline(idata,256); cout<< idata < At the end of the file it stops reading and continues with ur code... is that what u mean? /* Just a Human Trying to Live in a Computers World. */
-
Hello ! I have a little question regarding the >> operator of the ifstream. I overloaded the operator so that it will support streaming of my own classes but I want to secure this process by throwing an exception when an error occured during reading the file. For example, if we try to read when the end of the file is reached (so no more data to read), what happen with the ifstream class ? Does it throw an exception ? Or do I need to check for that before each read operation ? (This will be quite big because the classes I want to save are quite big)... Any help greatly appreciated :)
-
char idata[256]; ifstream a_file ( "data/text.dat" ); while (! a_file.eof() ) { a_file.getline(idata,256); cout<< idata < At the end of the file it stops reading and continues with ur code... is that what u mean? /* Just a Human Trying to Live in a Computers World. */
No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:
ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
{
i_stream>>MyClass.Data1;
i_stream>>MyClass.Data2;
....
....
return i_stream;
}But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when
i_stream>>
is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ? -
No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:
ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
{
i_stream>>MyClass.Data1;
i_stream>>MyClass.Data2;
....
....
return i_stream;
}But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when
i_stream>>
is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ? -
No, I'm not using getline but operator>>. In fact, the operator is redifined to accept my own classes, something loke that:
ifstream& operator>>(ifstream& i_stream,CMyClass MyClass)
{
i_stream>>MyClass.Data1;
i_stream>>MyClass.Data2;
....
....
return i_stream;
}But inside this function, I want to throw an exception when there is an error (for example end of file). So, what happens when
i_stream>>
is called and the stream is at the end of the file ? Is an exception thrown or do I need to check the errors myself each time (quite a lot of code 'cause a lot of data inside CMyClass) ? Or maybe I can do it just once at the end of the function, is that a valid solution ?Why are you doing it that way?... The Golden Rule in Programming: Keep It Simple Stupid. No offense, but its true... The code i jus posted will do everything ur doing, and make ur life alot easier... /* Just a Human Trying to Live in a Computers World. */
-
Why are you doing it that way?... The Golden Rule in Programming: Keep It Simple Stupid. No offense, but its true... The code i jus posted will do everything ur doing, and make ur life alot easier... /* Just a Human Trying to Live in a Computers World. */
Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:
ifstream is(.....);
CMyClass Class;
is>>Class;Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured
-
Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:
ifstream is(.....);
CMyClass Class;
is>>Class;Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured
oOoh i learned something today :) cant you still jus use the eof function like ur using getline? /* Just a Human Trying to Live in a Computers World. */
-
Why ? Because it is not text but binary data. And so, it would much easier after that. The code for loading a class would look like:
ifstream is(.....);
CMyClass Class;
is>>Class;Looks much compact isn't it ;-). And this is included in a try/catch statement to know if an error occured
-
end of file
is not an error. andgetline()
raiseseof()
the sameoperator>>()
does because it is the base class that manages it.
TOXCCT >>> GEII power
[toxcct][VisualCalc]Yes I know that end of file is not an error. But for me it should be an error if it happen before the complete class has been read :). It means that I try to load data from a file which is probably corrupted so I need to throw an exception. When is this flag raised by the way ? When the last byte of data is read or when you read past the last byte of data ? And what happens if you continue to read data even if the eof flag has been raised ? A solution could be to read everything and just check at the end of the function if the complete class has been read... Thanks for the suggestions
-
ifstream is(.....);
CMyClass Class;
do {
is>>Class;
while (!is.eof())isn't is as good ...
TOXCCT >>> GEII power
[toxcct][VisualCalc]It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)
-
It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)
Disreguard my stupidity and "newbie-ness", But isnt there a way to translate Binary to text and back to Binary?... Im lost now :( /* Just a Human Trying to Live in a Computers World. */
-
It won't compile because operator>> doesn't accept a parameter of type CMyClass ;). Thus, the need to provide the function thus the need to add secured reading ;)
-
oOoh i learned something today :) cant you still jus use the eof function like ur using getline? /* Just a Human Trying to Live in a Computers World. */
Yes I could but the class CMyClass contains a lot of variable (integers, float, ...) and this is quite boring for each of these variable to have something like that:
ifstream& operator>>(ifstream& is,CMyClass MyClass)
{
MyClass>>data1;
if (is.eof())
{
CFileException exception(....);
throw exception;
}MyClass>>data2;
if (is.eof())
{
CFileException exception(....);
throw exception;
}....
}Doesn't look really cool isn't it ? ;) But maybe I can just check this at the end od the function...
-
didn't you say that you overloaded th
operator>>()
for it can get a CMyClass parameter to read ? if you do so, it will compile, and THUS, is will work fine !!!
TOXCCT >>> GEII power
[toxcct][VisualCalc]Yes of course, but the question is not there :) The problem is not reading several CMyClass, the problem is securin the reading of ONE CMyClass :) In fact, I want to secure reading the file ! So, this is in the overloaded operator that the checking must be done, not outside. If this file is corrupted (or whatever, something wrong happens) it should throw an exception. So I want to check for end of file (and for other valid parameter) INSIDE the overloaded operator for each member variable of the class.
-
Disreguard my stupidity and "newbie-ness", But isnt there a way to translate Binary to text and back to Binary?... Im lost now :( /* Just a Human Trying to Live in a Computers World. */
Yes it could be but I have nothing to do with text... This is not used for printing data to the console but only reading/saving data from/to a file. So I'm just using binary data to load integers, floating, ... values
-
Yes of course, but the question is not there :) The problem is not reading several CMyClass, the problem is securin the reading of ONE CMyClass :) In fact, I want to secure reading the file ! So, this is in the overloaded operator that the checking must be done, not outside. If this file is corrupted (or whatever, something wrong happens) it should throw an exception. So I want to check for end of file (and for other valid parameter) INSIDE the overloaded operator for each member variable of the class.
-
ok. what about :
if (streamObj.eof())
throw CMyEOF;
else {
// Other cases ...
}
TOXCCT >>> GEII power
[toxcct][VisualCalc]Yes, that what I wanted to do but I don't want to check for eof between each member variables (and thus adding this code between each line... a little bit heavy). I think I will read all variables (even if eof is reached in between) and only check for errors at the end of the function. So to resume, is this flag raised when the last byte of data is read or when you try to read after the last byte ? In the first case, I will throw an exception when I will read the last structure in file even if it completely read (not goooood :) ). When you try to read data after the eof flag has been raised, is the failed bit raised ? If yes, then I can just check for fail() and raise an exception when it's true.
-
Yes, that what I wanted to do but I don't want to check for eof between each member variables (and thus adding this code between each line... a little bit heavy). I think I will read all variables (even if eof is reached in between) and only check for errors at the end of the function. So to resume, is this flag raised when the last byte of data is read or when you try to read after the last byte ? In the first case, I will throw an exception when I will read the last structure in file even if it completely read (not goooood :) ). When you try to read data after the eof flag has been raised, is the failed bit raised ? If yes, then I can just check for fail() and raise an exception when it's true.
i found this in the MSDN at that[^] page :
// ios_base_failure.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>int main() {
using namespace std;
fstream file;
file.exceptions(ios::failbit);
try {
file.open("rm.txt", ios_base::in);
// Opens nonexistent file for reading
}
catch(ios_base::failure f) {
cout << "Caught an exception." << endl;
}
}i hope this time it will help :-D:rose::cool:
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
i found this in the MSDN at that[^] page :
// ios_base_failure.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>int main() {
using namespace std;
fstream file;
file.exceptions(ios::failbit);
try {
file.open("rm.txt", ios_base::in);
// Opens nonexistent file for reading
}
catch(ios_base::failure f) {
cout << "Caught an exception." << endl;
}
}i hope this time it will help :-D:rose::cool:
TOXCCT >>> GEII power
[toxcct][VisualCalc]Looks interesting :)... I will look at this a little bit more in details, that could be really interesting if an exception is thrown on error ;) Nowaday, thanks for your help and time. That was an interesting conversation :)