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

Discussion in 'C++' started by sharat, Apr 16, 2012.

  1. sharat

    sharat Guest

    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();
     
    sharat, Apr 16, 2012
    #1
    1. Advertising

  2. sharat <> 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::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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Apr 16, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luigi Donatello Asero

    Are these pictures too dark or/and too large?

    Luigi Donatello Asero, May 19, 2004, in forum: HTML
    Replies:
    13
    Views:
    951
    Mabden
    May 21, 2004
  2. Luigi Donatello Asero

    Re: Are these pictures too dark or/and too large?

    Luigi Donatello Asero, May 21, 2004, in forum: HTML
    Replies:
    0
    Views:
    680
    Luigi Donatello Asero
    May 21, 2004
  3. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    642
    Ivan Johansen
    Jul 14, 2005
  4. shyam
    Replies:
    1
    Views:
    1,799
    Jack Klein
    Oct 10, 2006
  5. Michael Tan
    Replies:
    32
    Views:
    1,101
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page