473,770 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading large files

JS
Hello all!

I have come on to a problem for which I am not able to find a solution
searching the web.

What I am trying to do is reading a log-file with a size of 1.3 GB.
When reading it using fread() or the Visual C specific(?) read() they
return that the number of bytes read equals the number of bytes
requested, however all of the bytes read turn out to have a value of
zero (the file contains other values). When using the exact same code
on a much smaller file (1 MB) it works fine.

char filename[] = "c:/temp/data2000.dat";
char buffer[40];
FILE *file_d;
struct _stat stat_buf;
int fd;
int file_exists;
long file_size;

//Returns true:
file_exists = (_stat(filename ,&stat_buf)== 0) && (stat_buf.st_mo de &
S_IFREG);
//Returns 1328000000 (actual filesize):
file_size = (file_exists) ? stat_buf.st_siz e : -1;
//Booth returns values <> NULL and -1:
//file_d = fopen(filename, "r");
fd = open(filename, _O_RDONLY);

//Both returns 40:
//bytes_read = fread(buffer, 1, sizeof(buffer), file_d);
bytes_read = read(fd, buffer, 40);

//fclose(file_d);
close(fd);

Greatful for any ideas!
Best regards
Nov 13 '05 #1
2 16243
mi*********@hot mail.com (JS) wrote:
Hello all!

I have come on to a problem for which I am not able to find a solution
searching the web.

What I am trying to do is reading a log-file with a size of 1.3 GB.
When reading it using fread() or the Visual C specific(?) read() they
return that the number of bytes read equals the number of bytes
requested, however all of the bytes read turn out to have a value of
zero (the file contains other values). When using the exact same code
on a much smaller file (1 MB) it works fine.


<CODE SNIPPED>

Did you make sure your file contains pure text data? If it does not,
you should open it in binary mode.

Note that _stat(), open(), read(), and close() are not standard C
functions (Ah, I see from your remark above you already know ...).

Also note that both of us swapped the second and third argument of
fread, though this is useful to get the number of characters read in the
final call to fread, which will result most certainly in a partial
filled buffer. (It doesn't really matter anyway in this case, because
there's not a big difference between fread performing 40*1 or 1*40 calls
to fgetc respectively.)

The program below works fine for me (tested with a 2.8 GB file):

#include <stdio.h>
#include <stdlib.h>

#define BUFELEM 40
#define FILENAME "test.dat"

int main( void )
{
size_t num;
unsigned long count;
unsigned char buf[ BUFELEM ];
FILE *fp;

if ( ( fp = fopen( FILENAME, "rb" ) ) != NULL )
{
count = 0;
do
{
num = fread( buf, 1, BUFELEM, fp );
++count;
/* do sth. with buffer contents here [1] */
}
while ( num == BUFELEM );

/* Error checking snipped for brevity */

fclose( fp );
printf( "calls to fread: %ld\n", count );
}
else
{
fprintf( stderr, "Error opening file %s\n", FILENAME );
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

[1] You may write the buffer contents to another file and compare
it to the original to make sure valid data had ben read.
Regards

Irrwahn
--
The generation of random numbers is too important
to be left to chance.
Nov 13 '05 #2
On 19 Sep 2003 00:13:37 -0700, mi*********@hot mail.com (JS) wrote:
Hello all!

I have come on to a problem for which I am not able to find a solution
searching the web.

What I am trying to do is reading a log-file with a size of 1.3 GB.
When reading it using fread() or the Visual C specific(?) read() they
return that the number of bytes read equals the number of bytes
requested, however all of the bytes read turn out to have a value of
zero (the file contains other values). When using the exact same code
on a much smaller file (1 MB) it works fine.


<code snipped>

Have you verified that the beginning of the file is not a bunch of
null characters, by using a binary dump facility or something?

- Sev
--
I am just a thought of mine, an egotisticality. (P. Crews)
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
5281
by: Brad Tilley | last post by:
I have some large files (between 2 & 4 GB) that I want to do a few things with. Here's how I've been using the md5 module in Python: original = file(path + f, 'rb') data = original.read(4096) original.close() verify = md5.new(data) print verify.hexdigest(), f Is reading the first 4096 bytes of the files and calculating the md5 sum
5
3015
by: Paul | last post by:
I'm using Microsoft's ASPFileUpload routines and all works fine as long as my files are smaller than about 200K. For anything larger than about 210K, I get the following error: Error Type: Request object, ASP 0104 (0x80004005) Operation not Allowed On the .Upload method. Has anyone else experienced this problem with ASPFileUpload?
3
2289
by: Steven Burn | last post by:
The application; Service on my webserver that allows a user to upload their HOSTS file for functions to verify the contents are still valid. Uses; 1. XMLHTTP (MSXML2) 2. FileSystemObject 3. CrazyBeavers Upload control (couldn't get the Dundas one to work)
3
3175
by: Michael | last post by:
X-Replace-Address Hello, I am trying to write a program at work for reading/writing files larger than 4 GB. I know that Windows supports files that big but I have not been able to get my program to write past the 4 gig boundary. Some solutions that I have tried:
3
6470
by: Paul Spielvogel | last post by:
I need to compute the MD5 hash on VERY large files 500mb to 4gb+ I have found two ways but neither one of them does what i need. Private Function ComputeDataMD5(ByVal path As String) As String Dim fi As New FileInfo(path) Dim fs As FileStream = fi.OpenRead() fs = fi.OpenRead Dim Md5 As New MD5CryptoServiceProvider
3
3087
by: Eric Twietmeyer | last post by:
Hello, I have code written using the basic fstream object on a Win32 system. This is of course simply a typedef for basic_fstream<char, char_traits<char. This object can not be used to write or read files that are larger than 2Gb in size, as it would appear (from looking at the iosfwd header) that the underlying streamoff and streamsize types are typedef'd to long and int. On Win64 systems, these are typedef'd to __int64, so it seems...
1
1584
by: akalmand | last post by:
Hi there, I am writing a code to read some data from the text files. The number of text files is not fixed and could be more that 15. the length of each file is large... close to 100,000 on an average. some of them are extra large. The data that I have to read will always be at the bottom and will be in the last 5 -20 line in the files depending upon their size. small files will have 5 line and large files will have 20 lines to read. Can...
2
5454
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
17
9949
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream> #include <string> using namespace std; int main () {
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.