473,378 Members | 1,555 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,378 software developers and data experts.

Large Files

raj
Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?

Thanks for answering!

Dec 16 '07 #1
12 1563
raj wrote:
Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?
Go to groups.google.com and search comp.lang.c for messages with "large
files" in the name. The most recent occurrence was 2007-11-08.
Dec 16 '07 #2
"raj" <ra*@spamtrap.invalidwrote in message
Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?

Thanks for answering!
A long will give you 2G of space. Since you only need 4/5 G for the for the
"Hello" and another 4/5 for the "World" you are just within limits.

It would be prudent to check ferror after each call to fprintf / fwrite,
since it is not unlikely that the filesystem cannot support such large
files, or will run out of space. However it is just an ordinary C function
call job, not different in any way from if the requirement was to write 1K
or each.
That assumes a cross-platform question. Particular architecures may have
poor standard libraries that require special calls for large files. You
can't reasonably be expected to know all these details, though questioner
might not realise that - in which case it is tricky social but not technical
situation.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Dec 16 '07 #3
Malcolm McLean wrote:
"raj" <ra*@spamtrap.invalidwrote in message
>Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?

Thanks for answering!
A long will give you 2G of space.
Says who?

--
Ian Collins.
Dec 16 '07 #4
raj wrote:
Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?
Output 800000000 copies of "Hello", then output 800000000
copies of "World". Finally, use ferror() to see whether any
I/O errors occurred, and make sure fclose() succeeds before
your program declares success.

Notes:

1) The symbol "4GB" usually means 4294967296 to computer
people, but the task would be impossible if that were the
case in this instance: both "Hello" and "World" are five
bytes long, and 4294967296 is not divisible by five. Therefore
the prefix "G" presumably denotes its meaning under international
standards, namely, 1000000000. The assignment therefore calls
for 4000000000 bytes to be filled with each word, not 4294967296.
Besides making the task possible, this observation will make your
program run about seven percent faster; be sure to point this
out to the interviewer, who will be impressed with your devotion
to efficiency.

2) Since the task does not mention writing any newline
characters, the output cannot be a well-formed text stream
because each line of such a stream ends with a '\n'. (Even
on systems where an unterminated line is allowed, the length
of the generated line would exceed the portable limit.) So
we conclude that the output is to be a binary stream; keep
this in mind when you call fopen().

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 16 '07 #5
raj wrote:
Hi friends,

In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"

and the secod 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.

Does anybody know how?
#include <stdio.h>
#include <stdlib.h>

#define FNAME "big-file"

int main(void)
{
int rc=EXIT_FAILURE, i,j,k;
FILE *out = fopen(FNAME,"w+");

if (out != NULL)
{
printf("Writing 4Gb 'Hello' to file '%s'...\n", FNAME);

for (i=0; i<4*1024; i++)
for (j=0; j<1024; j++)
for (k=0; k<1024; k++)
fprintf(out, "%c", "Hello"[k%5]);

printf("Writing 4Gb 'World' to file '%s'...\n", FNAME);
for (i=0; i<4*1024; i++)
for (j=0; j<1024; j++)
for (k=0; k<1024; k++)
fprintf(out, "%c", "World"[k%5]);

fclose(out);
rc = EXIT_SUCCESS;
}
return rc;
}
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 16 '07 #6
Eric Sosman wrote:

[...]
>
Output 800000000 copies of "Hello", then output 800000000
copies of "World". Finally, use ferror() to see whether any
I/O errors occurred, and make sure fclose() succeeds before
your program declares success.
Good point, I forgot to call ferror()! :)
Notes:

1) The symbol "4GB" usually means 4294967296 to computer
people, but the task would be impossible if that were the
case in this instance: both "Hello" and "World" are five
bytes long, and 4294967296 is not divisible by five. Therefore
the prefix "G" presumably denotes its meaning under international
standards, namely, 1000000000. The assignment therefore calls
Not agreeing here, filling don't mean the last word has to be "Hello"
and "World".

Hence, if using the 1000x1000x1000 or the 1024x1024x1024 definition of
gigabyte, shouldn't make a difference.
2) Since the task does not mention writing any newline
characters, the output cannot be a well-formed text stream
because each line of such a stream ends with a '\n'. (Even
on systems where an unterminated line is allowed, the length
of the generated line would exceed the portable limit.) So
we conclude that the output is to be a binary stream; keep
this in mind when you call fopen().
Another good point.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 16 '07 #7
Tor Rustad <to********@hotmail.comwrites:
for (i=0; i<4*1024; i++)
for (j=0; j<1024; j++)
for (k=0; k<1024; k++)
fprintf(out, "%c", "Hello"[k%5]);
1024 is not evenly divisible by 5, so this will lead to a uneven
boundary between the end of one kilobyte of output and the start
of the next.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Dec 17 '07 #8
On Dec 16, 2:24 pm, raj <r...@spamtrap.invalidwrote:
In an interview I was asked to write a C program to create a large file
of 8GB

The first 4GB is filled with "Hello"
and the second 4GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.
The way to deal with 32 bits elegantly, is to use 64 bits:

#include <stdio.h>
#include <stdlib.h>
#include "pstdint.h" /* http://www.pobox.com/~qed/pstdint.h */

int write4GB (char * rept, FILE * fp) {
int64_t ofs;
size_t slen = strlen (rept);

for (ofs = slen;
ofs < INT64_C(4294967296);
ofs += slen) {
fprintf (fp, "%s", rept);
if (ferror (fp)) return -__LINE__;
}
rept[(size_t) (INT64_C(4294967296)+slen-ofs)] = '\0';
fprintf (fp, "%s", rept);
if (ferror (fp)) return -__LINE__;
return 0;
}

int main () {
char hello[] = "Hello";
char world[] = "World";
FILE * fp = fopen ("file.txt", "w");
int ret = EXIT_FAILURE;

if (fp) {
if (0 == write4GB (hello, fp) && 0 == write4GB (world, fp))
ret = EXIT_SUCCESS;
fclose (fp);
}
return ret;
}

You could solve this with 32 bits and a do { ... } while(), but you
know what? Life is too short, and you are IO limited anyways.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Dec 17 '07 #9

"Ian Collins" <ia******@hotmail.comwrote in message
Malcolm McLean wrote:
>A long will give you 2G of space.

Says who?
ANSI / ISO/
Dec 17 '07 #10
In article <__******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>
"Ian Collins" <ia******@hotmail.comwrote in message
>Malcolm McLean wrote:
>>A long will give you 2G of space.

Says who?
ANSI / ISO/
Perhaps you meant "at least" in your original statement.
The C standards permit larger long.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Dec 17 '07 #11
On Sun, 16 Dec 2007 23:12:57 -0800 (PST), Paul Hsieh wrote:
if (fp) {
if (0 == write4GB (hello, fp) && 0 == write4GB (world, fp))
ret = EXIT_SUCCESS;
fclose (fp);
}
return ret;
Ignoring the return value of fclose (fp) means that some error
conditons are reported as success.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Dec 17 '07 #12
Ben Pfaff wrote:
Tor Rustad <to********@hotmail.comwrites:
> for (i=0; i<4*1024; i++)
for (j=0; j<1024; j++)
for (k=0; k<1024; k++)
fprintf(out, "%c", "Hello"[k%5]);

1024 is not evenly divisible by 5, so this will lead to a uneven
5 isn't a factor in 2^64 either. :)
boundary between the end of one kilobyte of output and the start
of the next.
Yup, which was the reason I didn't print the whole word on each
fprintf() call.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 18 '07 #13

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

Similar topics

2
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
3
by: Buddy Ackerman | last post by:
I'm trying to write files directly to the client so that it forces the client to open the Save As dialog box rather than display the file. On some occasions the files are very large (100MB+). On...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
2
by: jdev8080 | last post by:
We are looking at creating large XML files containing binary data (encoded as base64) and passing them to transformers that will parse and transform the data into different formats. Basically,...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
17
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>...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.