Solved

Problem with reading a file

Posted on 2004-08-18
3
256 Views
Last Modified: 2013-11-20
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
Comment
Question by:ManojKrChauhan
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Nass89 earned 125 total points
ID: 11838680
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
 

Author Comment

by:ManojKrChauhan
ID: 11839950
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
 
LVL 5

Expert Comment

by:Nass89
ID: 11857817
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now