Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream Error: Failed to write file: Result is too large.

Reply
Thread Tools

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

 
 
sharat
Guest
Posts: n/a
 
      04-16-2012
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();
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      04-16-2012
sharat <(E-Mail Removed)> wrote:
> 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: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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to check write failure in ofstream?? shyam C++ 5 04-11-2006 04:45 PM
ofstream:seekp failed wobudui@gmail.com C++ 5 03-23-2006 09:17 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
how to write bits by using ofstream? Sowen C++ 8 03-18-2005 09:21 PM



Advertisments