How to create large files with c?

L

loudking

Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way.

Does anybody know how?

Thanks for answering!
 
R

Richard Tobin

loudking said:
My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way.

I don't think many people here will want to waste their time telling
you how to do such an obviously stupid (not to mention fictional) task.

If you want our help, try telling us what you really want to do.

-- Richard
 
I

Ian Collins

loudking said:
Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"
You have a very strange manager.
Sorry to say that I don't know how to do that in an elegant way.
The be inelegant, the task doesn't ask for elegance, spew out "hello"
1GB/sizeof("hello") times followed by "world" 1GB/sizeof("world") times.
Did he happen to note that you can't fit an exact number of either
"hello" or "world" into 1GB?
 
J

James Kuyper

loudking said:
Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

If a manager told you to do that, it could only be because he wants you
to learn something; there's no possible practical use for such a file.
Are you sure you don't mean "teacher" rather than "manager"?
Sorry to say that I don't know how to do that in an elegant way.

I'm curious - what is the inelegant way that you've rejected?
Does anybody know how?

Yes.
 
J

jacob navia

loudking said:
Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way.

Does anybody know how?

Thanks for answering!

Why C?

Open a command shell, then do:

dir >myfile
type myfile>>myfile
type myfile>>myfile

After less than 10 of those commands you have already 1MB.
After 20 you have...

Well that is an interesting question. How much of them you need
to get a file greater than 2GB?

You will be amazed. I think it could be even less than 32.

jacob
 
R

Richard Heathfield

jacob navia said:


See below.
Open a command shell, then do:

dir >myfile
type myfile>>myfile

I tried this on my system, but was told: "bash: type: myfile: not found"

One of the reasons we use C is for portability.

type myfile>>myfile

After less than 10 of those commands you have already 1MB.

....which, even on systems where it works, won't contain the required
contents, which are 1GB's-worth of "Hello" followed by 1GB's-worth of
"World".

So your reply consisted entirely of incorrect, non-portable advice. This
will come as no surprise to those who are familiar with your other
comp.lang.c articles.
 
K

Kenneth Brody

James said:
If a manager told you to do that, it could only be because he wants you
to learn something; there's no possible practical use for such a file.
Are you sure you don't mean "teacher" rather than "manager"?


I'm curious - what is the inelegant way that you've rejected?

Perhaps:

Create a file with "Hello" in it.
Write a function to write an input file twice to an output file.
Create a file with two "Hello"s in it by combining two copies of
the first file.
Repeat, creating a file with four "Hello"s by duplicating the
second file.
Repeat for 8, 16, 32, 64, and so on.
Finally, call your function to combine the previously-created
files such that you end up with a 1GB file.

Repeat for "World".

Combine the two 1GB files.

Remove all the temp files.


--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Bartc

loudking said:
Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way.

Does anybody know how?

Thanks for answering!

The following might do it. Don't know if it's elegant, but it's to the
point:

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

int main()
{
int i;
FILE* f;

f=fopen("output","wb");

if (f==NULL) return EXIT_FAILURE;

for (i=0; i<214748364; ++i) fprintf(f,"Hello");
fprintf(f,"Hell");
for (i=0; i<214748364; ++i) fprintf(f,"World");
fprintf(f,"Worl");

if (fclose==0) return EXIT_SUCCESS;
return EXIT_FAILURE;

}
 
D

David Tiktin

The following might do it. Don't know if it's elegant, but it's to
the point:

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

int main()
{
int i;
FILE* f;

f=fopen("output","wb");

if (f==NULL) return EXIT_FAILURE;

for (i=0; i<214748364; ++i) fprintf(f,"Hello");
fprintf(f,"Hell");
for (i=0; i<214748364; ++i) fprintf(f,"World");
fprintf(f,"Worl");

if (fclose==0) return EXIT_SUCCESS;

fclose is pretty well guaranteed to not be equal to 0 ;-) fclose(f)?
return EXIT_FAILURE;

}

Dave
 
B

Bartc

David Tiktin said:
fclose is pretty well guaranteed to not be equal to 0 ;-)
fclose(f)?

Yes. But no compiler warning and the program seemed to work. My docs say:
"fclose returns 0 if the stream is successfully closed".

Bart
 
D

David Tiktin

Yes. But no compiler warning and the program seemed to work. My
docs say: "fclose returns 0 if the stream is successfully closed".

It's perfectly legal C (no need for a warning unless you enable
checking for constant conditionals), and it *seems* to work, but it
doesn't do the right thing. Your program will always return
EXIT_FAILURE since fclose (*with no parentheses*) is a function
pointer which (if your program linked) will not be NULL, that is, 0.
So the test you coded should always fail. You want to test for the
return value of "fclose(f)", which actually calls fclose.

Dave
 
B

Ben Pfaff

jacob navia said:
Open a command shell, then do:

dir >myfile
type myfile>>myfile
type myfile>>myfile

After less than 10 of those commands you have already 1MB.

Hmm. If one replaces "type" by "cat", in a POSIX environment, I
suspect that it's implementation-dependent whether the first
concatenation command terminates before filling all available
storage on the file system.
 
T

Tor Rustad

loudking said:
Hello, all

My manager told me to create a large file of 2GB
The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Nice try, managers don't do that, but teachers do.

homework-o-meter = 99%
Sorry to say that I don't know how to do that in an elegant way.

Why not?
Does anybody know how?

Yup, given your file-system support 2 Gb files.
 
B

Bartc

I understand your comment now.
It's perfectly legal C (no need for a warning unless you enable

This is the first piece of C code I've ever posted (and one of the few I've
written for that matter), and in this small fragment already some pitfalls:

* Function calls must always end with (), empty or not. That's why no
warning of the missing parameter. Easy to miss off, coming from other
languages
* And if the () is missing, the call is interpreted as.. the address of the
function? Wouldn't a mandatory & in front be better? Then (fclose==0) would
raise an error.
* And if written as a statement, eg. fclose; this generates no code and no
warning!

Not using C much I must say all this is a surprise.

Bart
 
B

Ben Pfaff

Bartc said:
* Function calls must always end with (), empty or not. That's why no
warning of the missing parameter. Easy to miss off, coming from other
languages
* And if the () is missing, the call is interpreted as.. the address of the
function? Wouldn't a mandatory & in front be better? Then (fclose==0) would
raise an error.

If you turn the warning level of your compiler up high enough,
you can probably arrange to get a warning when you forget () in a
function call.
 
J

jaysome

If you turn the warning level of your compiler up high enough,
you can probably arrange to get a warning when you forget () in a
function call.

Right.

/* foo.c */
#include <stdio.h>
int main(void)
{
if ( fclose )
{
printf("fclose is not 0\n");
}
return 0;
}
gcc -Wall -W -ansi -pedantic -o foo foo.c
foo.c: In function 'main':
foo.c:5: warning: the address of 'fclose', will always evaluate as
'true'

Microsoft VC++ 6.0 and Visual Studio 2005
(http://msdn2.microsoft.com/en-us/express/):
foo.c(5) : warning C4550: expression evaluates to a function which is
missing an argument list

Comeau online compiler (http://www.comeaucomputing.com/tryitout/):
"ComeauTest.c", line 5: warning: controlling expression is constant
if ( fclose )
^

PC-lint (http://www.gimpel.com):
foo.c(5) : Warning 506: Constant value Boolean

Regards
--
jay

It is ironic that in its warning message gcc incorrectly separates the
subject (address) from the verb phrase (will always evaluate) with a
comma, in light of the fact that the "-pedantic" option was specified.
The moral of the story is that the "-pedantic" option in no way
contrues that gcc will be pedantic about English grammar. And neither
Microsoft nor Gimpel commit such an egregious faux paus in their
warning messages.
 
L

Laurent Deniau

Hello, all

My manager told me to create a large file of 2GB

The first 1GB is filled with "Hello"

and the secod 1GB is filled with "World"

Sorry to say that I don't know how to do that in an elegant way.

Does anybody know how?

If the file is < 2GB, the task is trivial (home work?). If the file is
2GB, which looks more serious, doing it portably is not trivial and
may require system depend configuration. For example on Linux x86 and
some other archs, you have to use/enable the Large File System API of
the glibc (see the manual) to have 64bits file offsets (i.e. fpos_t).

a+, ld.
 
C

Chris Torek

jaysome said:
if ( fclose )
[the following compilers produce the following warnings]
foo.c: In function 'main':
foo.c:5: warning: the address of 'fclose', will always evaluate as
'true'

Microsoft VC++ 6.0 and Visual Studio 2005
(http://msdn2.microsoft.com/en-us/express/):
foo.c(5) : warning C4550: expression evaluates to a function which is
missing an argument list

Comeau online compiler (http://www.comeaucomputing.com/tryitout/):
"ComeauTest.c", line 5: warning: controlling expression is constant
if ( fclose )
^

PC-lint (http://www.gimpel.com):
foo.c(5) : Warning 506: Constant value Boolean

[which are all accurate, as far as they go; he then notes in his
signature that:]
It is ironic that in its warning message gcc incorrectly separates the
subject (address) from the verb phrase (will always evaluate) with a
comma, in light of the fact that the "-pedantic" option was specified.
The moral of the story is that the "-pedantic" option in no way
contrues that gcc will be pedantic about English grammar.

Indeed (though you have a typo here -- you mean "construes" -- and
"construing" is something done by an entity that reasons, so you
probably should have said "implies" :) ).
And neither Microsoft nor Gimpel commit such an egregious faux paus
in their warning messages.

(In keeping with the grand tradition, this also has an error: it
should read "faux pas". :) )

Microsoft's warning does leave something to be desired, however.
As written, it claims that the expression evaluates to a function
-- it does not; it evaluates to a *pointer to* a function -- and
that the function in question is missing an argument list. The
function itself cannot be missing an argument list; only a *call
to* a function can be missing an argument list, and then only if
there is a prototype in scope for the call. Besides getting this
subtle distinction wrong, the Microsoft diagnostic also uses "which"
in a restrictive clause; some (many?) grammarians frown on such
uses, preferring the word "that".

(I find Comeau's warning the clearest and most succinct, myself.
GCC's is the second clearest, accurately pointing out that fclose
is never called, but containing the odd sentence structure you
noted.)

Note: since the above is largely commentary on English language
usage, it must necessarily contain errors of spelling, typography,
and/or grammar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top