|
end of file is not an error.
and getline() raises eof() the same operator>>() 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
|
|
|
|
|
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
|
|
|
|
|
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...
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
ok.
what about :
if (streamObj.eof())
throw CMyEOF;
else {
}
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.
|
|
|
|
|
i found this in the MSDN at that[^] page :
#include <iostream>
#include <fstream>
int main() {
using namespace std;
fstream file;
file.exceptions(ios::failbit);
try {
file.open("rm.txt", ios_base::in);
}
catch(ios_base::failure f) {
cout << "Caught an exception." << endl;
}
}
i hope this time it will help
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
|
|
|
|
|
it doesn't throw an exception, but it raises a flag you can test with istream::eof()
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
hi:
I can remote debug MFC code thought tcp/ip with VC6 .
But I cann't do it with VC7.
I set the remote IP address in project settins ,and copy msvcmon.exe... to remote PC and run it.
then I press F5,but it didn't work.
Are there have any difference settings?
thanks answer.
|
|
|
|
|
Did you check the network and firewall settings?
Don't try it, just do it!
|
|
|
|
|
Alright... Using fstream when making a simple database using MFC... Say you want to make every letter another letter (coding)... Is it possible?
<br />
void CNewAccountDlg::OnOK() <br />
{<br />
CString strUsername;<br />
CString strPassword;<br />
GetDlgItemText(IDC_USERNAME,strUsername);<br />
GetDlgItemText(IDC_PASSWORD,strPassword);<br />
<br />
ofstream a_file ( "data/account.dat", ios::ate, ios::app);<br />
a_file<< strUsername + " " + strPassword + "\n";<br />
a_file.close();<br />
AfxMessageBox("Account Created.");<br />
}<br />
Nothing wrong with that... Makes a simple file with Username and Password. How could i break my GetDlgItemText into single CHAR allowing me to change them and regroup them back together? Is it possible to break down a String into single Characters?
/* Just a Human Trying to Live in a Computers World. */
|
|
|
|
|
NewbieStats wrote:
GetDlgItemText into single CHAR allowing me to change them?Is it possible to break down a String into single Characters?
Look for CString::GetAt()
NewbieStats wrote:
regroup them back together? Is it possible to break down a String into single Characters?
look at CString::SetAt() ,But beware in this allocate a empty buffer to CString before using CString::SetAt() function
[Vote One Here, Complete my Survey....]
Alok Gupta visit me at http://www.thisisalok.tk
"I Think Believe this Will Help"
|
|
|
|
|
Would it be Easier to do that or use;
strPassword.SetSel(0,1); // ++
Copy and Paste to New CString and Add all CStrings together?...
Seems like a long process, but would it be better?
/* Just a Human Trying to Live in a Computers World. */
|
|
|
|
|
|
Okay thanks ill try them both and see which one seems to fit better
/* Just a Human Trying to Live in a Computers World. */
|
|
|
|
|
Hi
I have developed one Multithreading application in C++, which runs on Windows 2000 Professional OS, in which I want to create some 10,000 thread. Is it possible to create these many threads ?
Is there any restriction on how many number of threads can we carete ?
How can I achieve this goal ?
Any suggestion plz...
Thankx a lot
Amarelia Maehsh
Gujarat
India
|
|
|
|