Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Problem with reading a file

Hi Experts

Sometimes i found a very strange problem when i read from a file.
I open a file in read mode as well as binary mode. Let i have a structure which has three fileds. each field has size of 4 bytes each. so total size of structure is 12 bytes.
I'm using _read() function to read from file. _lseek() is used to seek in file. suppose i want to read from offset 0x2100 in file.

_lseek(fp,0x2100, SEEK_SET); where fp is int fp;(file descriptor)
_read(fp,buf,12);     buf is object/variable of structure. size of buf is 12 bytes.

first time i read 12 bytes at offset 0x2100. then i read next 12 bytes, then next 12 .....
  Most time function is successful. say after reading 12 bytes 6 times,when i read next 12 bytes sometimes function read 4 or 5 bytes and sometimes it read no byte while file has data at this particular offset.  I have used #progrma pack(push,1) and #progrma pack(pop). but no success. I have tried by using fseek function in which i take FILE pointer to read from file but same problem. I don't know how to solve this problem. so please help me in this regard.
                                                    Thanks






0
ManojKrChauhan
Asked:
ManojKrChauhan
  • 2
1 Solution
 
Nass89Commented:
Hi,
Did you try to check the return values from _seek and _read?

----------------------------
Return Value

_lseek returns the offset, in bytes, of the new position from the beginning of the file. _lseeki64 returns the offset in a 64-bit integer. The function returns  –1L to indicate an error and sets errno either to EBADF, meaning the file handle is invalid, or to EINVAL, meaning the value for origin is invalid or the position specified by offset is before the beginning of the file. On devices incapable of seeking (such as terminals and printers), the return value is undefined.
----------------------------
Return Value

_read returns the number of bytes read, which may be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–linefeed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.

If the function tries to read at end of file, it returns 0. If the handle is invalid, or the file is not open for reading, or the file is locked, the function returns –1 and sets errno to EBADF.
----------------------------
Check the return values or send some codes.

Good Luck!
0
 
ManojKrChauhanAuthor Commented:
Ok i'm sending some code. Let i have a structure

/////////////////////////////////////
struct record{
                  int a;
                 int b;
                int c;
               };
struct record myrecord;
int fp;     //file descriptor
int byteread;
int offset=2100;

fp = _open(filename, _O_RDONLY));

// file contains name of file with its path. The file's size is greater than 500kb
//now to read data from file, there is a function readdata

 byteread=readdata(fp,&myrecord,offset);
offset=offset+12;
while(myrecord.a!=0)
{
 byteread=readdata(fp,&myrecord,offset);
offset=offset+12;
}

definition of function is      
         int  readdata(int fp,void *buffer,int offset)
               {
                int x;
                memset(buf,0x00,12);
                _lseek(fp, offset, SEEK_SET);
              x      =_read(fp,buf,size);
             return x;
            }

///////////////////////////////////////
offset may have any value. But offset value is not greater than file size. so _lseek is successful. and file has data at this offset. Suppose i want to read 12 bytes starting from offset 2100. In while loop, each time i read next 12 bytes until first 4 bytes are not 0.
since i read 12 bytes each, i get 12 bytes in buffer and i read until first 4 bytes are not 0 in the file. But i found, sometime function doesn't read all 12 bytes in  buffer and sometimes it read no byte(0 byte) while file has data at this location. I'm sure because i see file contents in a software "WinHex".
And this is not the problem with single file. I found such a problem many time for different files.
0
 
Nass89Commented:
Hi,
You wrote:
>>I open a file in read mode as well as binary mode.
I see in your codes no binary operation. To open the file in binary mode you need to write:

fp = _open(filename, _O_RDONLY |_O_BINARY));

I think your problem is that, your file is not opened in Binary mode. When you reach in file to CR, the return string will be shorter than you expect.
Also add a check for No. of bytes read in return value of readdata to break the file read.

One point, in readdata buf is not defined.

Thanks for the points.

Good Luck!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now