开发者

Unable to read data from a file in binary mode, C language

开发者 https://www.devze.com 2023-03-15 07:23 出处:网络
I am having a problem when I try to open a file to read in binary mode. The code is this: PT_LONG LoadDataFromFile1(const char *pFileName,

I am having a problem when I try to open a file to read in binary mode.

The code is this:

PT_LONG LoadDataFromFile1(const char *pFileName,
                          unsigned char **ppBufer, PT_LONG *pLenData )
{
    PT_LONG retVal = 0;
    PT_DWORD fileLength = 0,len,i;
    unsigned char *buff,fn[35];
    unsigned char buff1[500];
    FILE *fil = NULL;

    fil = fopen(pFileName, "rb" );
    if ( fil != NULL )
    {
        fseek( fil, 0, SEEK_END );
        fileLength = ftell( fil );

        if ( fileLength > 0 )
        {
            *ppBufer = (unsigned char *) malloc( fileLength );
            *pLenData = (PT_DWORD) fileLength;
            printf("\n\n test file length:%d\n\n",fileLength);
            fseek( fil, 0, SEEK_SET );
            fread( *ppBufer, 1, fileLength, fil );                         
        }
        else
        {  
            retVal = -2;
        }
        fclose( fil );
    }
    else
    {  
        retVal = -1;
    }
    return retVal;
}

Here

if 开发者_StackOverflowpFileName=/mnt/jffs2/bir/1234.bir it's working fine. But if the pfilename=/mnt/jffs2/bir/11772213A7894568.bir I am unable to read the data from the file. It returns -1.


If one file works and the other returns with -1 (File can't be opened in your case) it's 99% a problem with your file path :-) Please double-check the path to the file.


How are you getting the file names?

I suspect one of them has an attached trailing newline and you really try to open ".../something.bir\n" rather than ".../something.bir"

I suggest you write a trim function (or two: ltrim and rtrim) and process the filename before opening it.

0

精彩评论

暂无评论...
验证码 换一张
取 消