Fread() fails with ferror value 32 i.e. sharing violation :

Respuesta propuesta Fread() fails with ferror value 32 i.e. sharing violation :

  • miércoles, 29 de febrero de 2012 9:30
     
      Tiene código

    Hi,

    we are getting fread failure ONCE OR TWICE after so many successful executions.

    i even checked for end of file but it is not occured.

    ferror returns 32 i.e. sharing violation. Is there any 100% fix which can resolve this??

    could anyone please help me out?



    Thanks in advance for looking into this!!

    --Milind




    • Editado MilindDukre miércoles, 29 de febrero de 2012 9:32
    • Editado MilindDukre miércoles, 29 de febrero de 2012 9:40
    • Editado MilindDukre miércoles, 29 de febrero de 2012 10:30
    •  

Todas las respuestas

  • miércoles, 29 de febrero de 2012 10:22
     
     

    You shouldn't really be examining GetLastError() after an fread call - the proper diagnostic value for fread is errno.  The 'LastError' value may be being set somewhere within the call, but you have no way of knowing whether the fread() call has compensated for it afterwards. Check errno instead.

    That said, I wonder whether you are experiencing being locked out by another call: in the Remarks section of  fread, it states:

    This function locks out other threads. If you need a non-locking version, use _fread_nolock.


    Answering policy: see profile.

  • miércoles, 29 de febrero de 2012 10:47
     
     

    yes you are right! GetLastError does not give error code; instead it returns 0;

    but when i printed the value of ferror(fp), it gives 32 and it clearly means Sharing violation. hence i may not need errno to print.

    i am thinking of the solution where we can fclose() the file handle and reopen it using fopen() in read mode and again try for fread(),

    would it be feasible and fix the problem??

    also i would like to hear what effect will be made by using _fread_nolock?


    • Editado MilindDukre miércoles, 29 de febrero de 2012 10:55
    •  
  • miércoles, 29 de febrero de 2012 15:42
     
     Respuesta propuesta Tiene código

    1) ferror() does NOT necessarily return the value of errno.  It only states that it returns a non-zero error if an error has occurred on the stream.  Effectively, all you should do with it is use the return value in a boolean expression such as

    if (ferror(fp))
    {
        // handle error ...
    }

    2) According to errno, _doserrno, _sys_errlist, and _sys_nerr, a value for errno of 32 corresponds to EPIPE, "Broken pipe".

    3) _fread_nolock is unlikely to help you, especially as we do not yet know the actual error.

    4) Editing your original post to remove the code makes a nonsense of my reply.  Please don't do this in future.


    Answering policy: see profile.

  • jueves, 01 de marzo de 2012 5:02
     
     

    sorry for inconvenience caused.. i removed it due to some confidential matter!!

    so what will be the reason of occuring such error once in a day? any corrective action to be taken when such error occurs?

     
  • jueves, 01 de marzo de 2012 11:44
     
     
    You still have not told us what the value of errno is when it fails, so I doubt we can help you until you tell us.

    Answering policy: see profile.

  • jueves, 01 de marzo de 2012 11:50
     
     

    Hi David,

    My apology but,

    we need to make changes with respect to what we had in our hands, i mean what we have got from the current design.

    so there are no chances to see errno since this problem is occuring at customer side abroad and we have to provide support it with respect to provided logs.

    'it will be great if we succeed in that!!

    Thanks for your continuous help!! but need more than that ;) hope you can understand! :)

  • miércoles, 07 de marzo de 2012 8:28
    Moderador
     
     

    Hi MilindDukre,

    I am checking the thread status on my side? Would you mind letting me know the result of these suggestions? If the issue has been solved, please mark the reply which helped you as answer. Or you can post your solution here if you have got is work by yourself. If you need further help, please feel free to let us know. We'll continue working with you on this issue.

    Good Luck!
    Helen Zhao


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

  • miércoles, 07 de marzo de 2012 8:56
     
     

    i have not got any solution yet, i was just guessing the possible solution and would proceed with it only after confirmation. 

    anyone havin idea about this will help a lot!! thanks for looking into this :)

  • miércoles, 07 de marzo de 2012 11:42
     
     

    What are you trying to read ? a local file ?

    What parameters are you used with  fopen ?  Perhaps you try to read too faster, and need wait time to the object be ready...

    Perhaps error occurs realy on the objects you try to read ?

    Why not prefering use Createfile et Readfile, instead of fopen and fread that manage sharing problem?


    Delphine GARRO


  • miércoles, 07 de marzo de 2012 12:14
     
     

    yes, I am trying to read a local file. 

    i am opening the file in read binary mode only. we can use CreateFile instead ,but is there any solution for opening file using fopen(). To add more into this when I printed the ferror(fp) value it is giving 32. Do you have any solution for this? I am planning to reopen the file handle once more and try to read file once again.. let me know in case this wont work or any other issue?

  • miércoles, 07 de marzo de 2012 13:49
     
     

    I'm not sure that error code 32 means SHARING VIOLATION because ,

    ferror() generate different errors code (associated with the file errno.h ) than System Error Code:

    http://msdn.microsoft.com/fr-fr/library/y2wc3w90(v=vs.80).aspx

    http://msdn.microsoft.com/fr-fr/library/t3ayayh1(v=vs.80).aspx

    http://msdn.microsoft.com/fr-fr/library/5814770t(v=vs.80).aspx

    To have a real system error code, you must call the function GetLastError().

    Perhaps the file is too bigger: more than 2GB ?


    Delphine GARRO

  • viernes, 09 de marzo de 2012 5:08
     
     

    hi, 

    The immediate GetLastError() after fread() returns 0 means no error. is there any 'common way' to recover from fread failure?

    Please advise.

  • viernes, 09 de marzo de 2012 9:27
     
     

    Could give same lines of your code that use fopen and fread ?

    Have you simply verify the buffer used to read date ? memory leak? Buffer overrun?

    Do you you open file just before read and close it immediately after ?

    Have you many threads that read this file or only one task ?

    Which compiler are you using ? And for Wich winodws ?


    Delphine GARRO

  • viernes, 09 de marzo de 2012 9:35
     
      Tiene código
    fp = fopen(abcFile,"rb");
    if(fp == NULL )
    return 0;
     int num = 0;
    num = fread(buf,1,size,fp);
    
    if(num!=size)
    return 0;
    else 
    return 1;
    

    Please find some code snippets here..

    yes before reading i allocate 'buf' as of the 'size' of file.

    no, i dont close file pointer before fread().

    only one thread from my application is using this file.

    I am using visual c++ 6.0 and on XP OS.

  • viernes, 09 de marzo de 2012 12:20
     
     

    Delphine,

    if you read my earlier comments, you will see that that in this case, GetLastError() is unreliable - the one place we can look for the error is errno.  I thought that I had explained clearly that the return value of ferror(fp) is only useful for testing whether there had been an error on fp, not what the error was.  The OP's emphasis on 'error 32' is incorrect - it is not a diagnostic value in that sense - it just tells him that there was some kind of error.


    Answering policy: see profile.

  • viernes, 09 de marzo de 2012 12:45
     
     

    Either your program is corrupted and create stange behaviour, due to out of memory, or access violation

    Either you have an hardware error:

    - bad sector on your hard drive ( so try to copy the file you want to read on an other disk)

    - file system NTFS corrupted

    -problem of bus controler: EIDE or SATA ?

    So I think you should try your program on an other computer to determine either is due to your program (software problem) either to your computer (hardware problem )


    Delphine GARRO