ofstream Error: Failed to write file: Result is too large.

S

sharat

Hi ,

I facing problem while writing into file..
For my problem, File path withh be available after some time. So to
avoide "File does not exist " error I am sleeping in a loop for some
time ana again try to open a file. If file open successfully I will
write into file else through an exception.

If flow enters into while loop , and got the file after Sleep for few
seconds then problem is comeing ..
Failed to write to File: Result is too large"
where as if it does not go into while loop then it is able to create
file and write into it...
I am unable to find the reason. Can anybody help please???

Sample code :
int retryLoopCount = 0;
ofstream myfile;

myfile.open ((const char*)filePath);
while(! (myfile.is_open()))
{


Sleep( (retryLoopCount*2+1)*1000);

if(retryLoopCount++ > RETRY_COUNT)
{
int erCode = errno;
err.Format ("Failed to open file '%s' for writing: %s"
, (const char*)filePath
, strerror(erCode)
);
myfile.close();
throw Exception ------ (// throw Exception -- - I have not copied
the exact sample code here)
}
myfile.open ((const char*)filePath);

}// while retry.

myfile<< (const char*)content;

if (myfile.bad() ) {

int erCode = errno;
err.Format ("Failed to write file '%s': %s"
, (const char*)filePath
, strerror(erCode)
);
myfile.close();
throw Exception ----- (// throw Exception here -- I have not copied
the exact sample code here )
}

myfile.close();
 
J

Jens Thoms Toerring

sharat said:
I facing problem while writing into file..
For my problem, File path withh be available after some time. So to
avoide "File does not exist " error I am sleeping in a loop for some
time ana again try to open a file.

Can you please explain why you would have to try repeatedly
to open a file? If it fails something is wrong and waiting
and then retrying usually doesn't help (well, except if there
are permission problems and you manually fix them while the
program is waiting). So I don't understand what this is all
meant to be good for.
If file open successfully I will
write into file else through an exception.
If flow enters into while loop , and got the file after Sleep for few
seconds then problem is comeing ..
Failed to write to File: Result is too large"

"Result is too large" smells like an errno of ERANGE, something
which isn't something I would expect from I/O trouble...
where as if it does not go into while loop then it is able to create
file and write into it...
I am unable to find the reason. Can anybody help please???

Drop all that repeatedly trying to open the file (or, please,
give some explanation why you think it's necessary) - some-
thing strange must be going one here if it's needed anyway.
Sample code :
int retryLoopCount = 0;
ofstream myfile;
myfile.open ((const char*)filePath);

You can make that just

std::eek:fstream myfile( filePath );

And why the C-style cast? Or is 'filePath' perhaps not a char
array or pointer? Should that be the case (e.g. 'filePath' is
a pointer to a std::string) then the cast will just result in
additional grieve.
while(! (myfile.is_open()))
{
Sleep( (retryLoopCount*2+1)*1000);
if(retryLoopCount++ > RETRY_COUNT)
{
int erCode = errno;
err.Format ("Failed to open file '%s' for writing: %s"
, (const char*)filePath
, strerror(erCode)
);
myfile.close();
throw Exception ------ (// throw Exception -- I have not copied the exact sample code here)

If you want help it's typically better to post your actual code
instead of something you think is at the heart of the problem -
you could be wrong and may have left out the important bits.
myfile.open ((const char*)filePath);
}// while retry.
myfile<< (const char*)content;

Why the C-style cast? The '<<' operator will work fine on a
non-const char array or a pointer to a non-const char array.
Or is 'content' something else?
if (myfile.bad() ) {
int erCode = errno;
err.Format ("Failed to write file '%s': %s"
, (const char*)filePath
, strerror(erCode)
);
myfile.close();
throw Exception ----- (// throw Exception here -- I have not
copied the exact sample code here )
}
myfile.close();
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top