473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Limits on _read

emaghero
Evening all,
I have a quick question about the _read function. I am trying to read in a large data set into a character buffer. The data is stored in binary format. The size of the input data file is approximately 7 Mb, I have declared that the buffer should be 10 Mb. The code used is below, I am using MSVS 2003 in C++.
Expand|Select|Wrap|Line Numbers
  1. char BUF[1024*1024*10];//10 Mb buffer
  2.  
  3. int main(){
  4.  
  5.          char *buf=BUF;//pointer to the buffer
  6.  
  7.          int file=_open("file.dat",_O_RDONLY,_S_IREAD);
  8.  
  9.          if(file==-1){
  10.                     perror("Failed to open file");
  11.          }
  12.          else{
  13.                 cout<<"File is open\n";
  14.  
  15.                 int bytes;
  16.                 unsigned int nbytes=(1024*1024*10);
  17.  
  18.                bytes=_read(file,(void *)buf,nbytes);   
  19.          }
  20. }
  21.  
This code works to some extent. However, no matter what value I give for nbytes, the variable bytes always returns 112. This is causing me some concern as it is evident that all the data is not being read into the buf. Whenever I count into the buffer beyond a certain value of bytes I cannot access the data that I know should be there.

Is there a limit to the size of a file that can be read into a buffer using _read?

Can anybody suggest a solution to the problem?
Sep 6 '07 #1
3 3078
RRick
463 Expert 256MB
Check how you are opening the file. You can open a windows file as binary or text. Make sure it is opened as binary. If its opened as a text file, Ctl-Z will act as an EOF. This sounds like what is happening.

Check this link for more info:
http://msdn2.microsoft.com/en-us/......../en-us/library/wyssk1bs(VS.80).aspx
Sep 6 '07 #2
Check how you are opening the file. You can open a windows file as binary or text. Make sure it is opened as binary. If its opened as a text file, Ctl-Z will act as an EOF. This sounds like what is happening.

Check this link for more info:
http://msdn2.microsoft.com/en-us/library/wyssk1bs(VS.80).aspxhttp://msdn2.microsoft.com/en-us/library/wyssk1bs(VS.80).aspx
Unfortunately that didn't work. It was suggested to me that I should try to read it in multiple times to different locations in the buffer each time. Would this alleviate the problem?
Sep 6 '07 #3
RRick
463 Expert 256MB
Probably. I'm raising a caution flag here because you're not fixing the problem, but circumventing it. _read appears to have a mind of its own, and can ignore/skip some characters. With a binary file, this is bad, crash and burn bad.

That said, you can loop _read until you get an EOF. You can also place all of the data in a single buffer by updating a buffer pointer with the number of read bytes read and passing that pointer back to _read.

If you plan to go this route, I would check it with an ascii file with lots of <cr><lf>. _read will keep one but not both characters. Check and see if the lengths of the file and buffer are the same. If not, then you're back to the open as text instead of binary.
Sep 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Maurice LING | last post by:
Hi, I think I've hit a system limit in python when I try to construct a list of 200,000 elements. My error is malloc: vm_allocate (size = 2400256) failed...... Just wondering is this...
0
by: Randall Sell | last post by:
Hello all, I am migrating a Paradox application to SQL Server. My problem is that the existing Paradox table structure is limited. Correcting it will mean a re-write of the application (Delphi...
4
by: platho | last post by:
Hello, I bounced into the max 25 columns index limits on DB2 v7.2 on NT. Is this still so in other operating systems or in v8 ? Are there plans to change this in the future ? Any workarounds...
16
by: Mark Bruno | last post by:
Hey, I'm learning about the limits header, and I don't understand one snippit of code. If I code: #include <limits.h> #include <stdio.h> int main() { printf("Smallest signed long long:...
2
by: Jasim | last post by:
Hi, I'm a newbie C programmer, and I've written a Buffered File Copy program in C (which can be considered my rather first acceptable venture into the bold brave world of C) I am using...
37
by: Carol Depore | last post by:
How do I determine the maximum array size? For example, int a works, but a does not (run time error). Thank you.
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
13
by: Josip | last post by:
I'm trying to limit a value stored by object (either int or float): class Limited(object): def __init__(self, value, min, max): self.min, self.max = min, max self.n = value def...
44
by: vippstar | last post by:
n1256.pdf (C99 TC3), 5.2.4.1 Translation limits p1 says: Does that mean that *any* program using an array of two or more elements other than char is allowed to be rejected by an implementation?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.