Solved

Problem with reading a file

Posted on 2004-08-18
3
255 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

24 Experts available now in Live!

Get 1:1 Help Now