Solved

Problem with reading a file

Posted on 2004-08-18
3
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 59
bobThere java chalenge 45 97
Pressing cancel button with C# UI Automation 3 101
Change to event 1 136
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

734 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