By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

simple file system

P: n/a
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

Thanks

Greg
Nov 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a


Greg wrote:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?


What kind of garbage? Has it to be some pseudorandom stuff or
can it be the ramblings of the most infamous posters of this
newsgroup or some garbage pattern like "Garbage!" or ...?

I would suggest a look at fopen(), fclose(), fwrite() from
the standard library.
OTOH, as you want to give the world a new file system, this may
be exactly what you don't want to do. Fast solutions for your
specific system may necessitate leaving the realms of standard C.

IMO, it would be good if you elaborate what you mean by "best way"
and "garbage".
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2

P: n/a

"Greg" <dj*********@snowboard.com> wrote in message
news:7e*************************@posting.google.co m...
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?
FILE*
open_garbage(const char* filename, size_t size)
{
FILE* fp = fopen(filename, "w");

for(i=0; NULL != fp && i<size; i++)
{
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
{
fclose(fp);
fp = NULL;
}
}

return fp;
}
Thanks


My pleasure.
Nov 14 '05 #3

P: n/a
Neo

"Greg" <dj*********@snowboard.com> wrote in message
news:7e*************************@posting.google.co m...
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?


dd if=/dev/zero of=myfile.dat bs=1024 count=1024
this command will create a 1 MB file. all contents with zero.
U can use /dev/mem as input file for some junk data or any other device u
like...

-Neo

Nov 14 '05 #4

P: n/a
Greg wrote:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

Thanks

Greg


If you are using Linux you can transfer data from the device /dev/random
which is set up to provide random data for you. Check, but I suspect Unix
has this also.
Nov 14 '05 #5

P: n/a
On Thu, 25 Nov 2004 15:23:36 +0530, "Neo"
<ti***************@yahoo.com> wrote in comp.lang.c:

"Greg" <dj*********@snowboard.com> wrote in message
news:7e*************************@posting.google.co m...
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?


dd if=/dev/zero of=myfile.dat bs=1024 count=1024
this command will create a 1 MB file. all contents with zero.
U can use /dev/mem as input file for some junk data or any other device u
like...


Doesn't work on CP/M. Has nothing to do with the C language. Has
nothing very much to do with the English language, either.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6

P: n/a
"dandelion"
"Greg" I am trying to write a simple file system using c. It will consist of frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?


FILE*
open_garbage(const char* filename, size_t size)
{
FILE* fp = fopen(filename, "w");

for(i=0; NULL != fp && i<size; i++)
{
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
{
fclose(fp);
fp = NULL;
}
}

return fp;
}

Would you mind elaborating on what happens within that fputc call? MPJ
Nov 14 '05 #7

P: n/a
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?
Nov 14 '05 #8

P: n/a
On 25 Nov 2004 00:45:05 -0800, in comp.lang.c , dj*********@snowboard.com
(Greg) wrote:
My first question is: what is the best way to create a file and fill
it with garbage?


Install Windows 95?
Ah, sorry, no, that fills your entire filesystem with garbage. My mistake.

(gd&r)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #9

P: n/a

"Greg" <dj*********@snowboard.com> wrote in message
news:7e**************************@posting.google.c om...
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?


Make sure you specify binary mode when calling 'fopen()'.

-Mike
Nov 14 '05 #10

P: n/a
In article <41***********************@dreader14.news.xs4all.n l>,
dandelion <da*******@meadow.net> wrote:
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))


That doesn't look very random to me... If RAND_MAX is the maximum positive
int, it will produce a lot of zeros.

(And presumably you mean rand() rather than random() for standard C.)

-- Richard
Nov 14 '05 #11

P: n/a
Greg wrote:
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?


Strange. When I make a program out of it..

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

int main(void) {
FILE *fp;
int i, fs_size = 1024;
fp = fopen("greg.bin", "wb");
for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc(random() * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}
return 0;
}

...it creates a file of 1024 bytes zero.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #12

P: n/a

"Merrill & Michele" <be********@comcast.net> wrote in message
news:1r********************@comcast.com...
"dandelion"
"Greg" I am trying to write a simple file system using c. It will
consist
of frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?


FILE*
open_garbage(const char* filename, size_t size)
{
FILE* fp = fopen(filename, "w");

for(i=0; NULL != fp && i<size; i++)
{
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
{
fclose(fp);
fp = NULL;
}
}

return fp;
}

Would you mind elaborating on what happens within that fputc call? MPJ


The actual garbage (in the form of a call to random()) is generated. The
result of random() is [0..RAND_MAX], so there is a touch of simple
arithmatic to reduce that interval to [0..256] (which should have been
[0..255], come to think of it), which is written to the stream by fputc()
cast to unsigned char.
Nov 14 '05 #13

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:v1*****************@newsread1.news.pas.earthl ink.net...

"Greg" <dj*********@snowboard.com> wrote in message
news:7e**************************@posting.google.c om...
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?


Make sure you specify binary mode when calling 'fopen()'.


OP indicated UNIX as platform. Not DOS.
Nov 14 '05 #14

P: n/a

"Joe Wright" <jo********@comcast.net> wrote in message
news:hM********************@comcast.com...
Greg wrote:
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?


Strange. When I make a program out of it..

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

int main(void) {
FILE *fp;
int i, fs_size = 1024;
fp = fopen("greg.bin", "wb");
for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc(random() * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}
return 0;
}

..it creates a file of 1024 bytes zero.


I *told* you it was garbage... ;-)

You're correct of course. I missed some parenthesis.

(random() * 0x0100) / RAND_MAX.
Nov 14 '05 #15

P: n/a

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:co**********@pc-news.cogsci.ed.ac.uk...
In article <41***********************@dreader14.news.xs4all.n l>,
dandelion <da*******@meadow.net> wrote:
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
That doesn't look very random to me... If RAND_MAX is the maximum

positive int, it will produce a lot of zeros.

(And presumably you mean rand() rather than random() for standard C.)


I stand corrected.
Nov 14 '05 #16

P: n/a
In article <41*********************@dreader4.news.xs4all.nl >,
dandelion <da*******@meadow.net> wrote:
(random() * 0x0100) / RAND_MAX.


That still produces zero (if random() returns the full range of
positive integers). What are you trying to achieve?

rand() & 0xff will return an 8-bit random number. If you're worried
about the quality of rand(), shifting it right a little helps in some
implementations.

-- Richard
Nov 14 '05 #17

P: n/a

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:co***********@pc-news.cogsci.ed.ac.uk...

<snip>
That still produces zero (if random() returns the full range of
positive integers). What are you trying to achieve?
Prooving i'm an idiot, and i'm succeeding at it, seemingly.

Write it down to lots of work and a bit of frustration about [*(&!*@&#!@
GRRRR] hardware faults, a buggy development platform and *(&#*&$@ customers
with vague specs which change from week to week.

Seemingly my mind wasn't on it when I wrote that piece of *(&@#&$@!!!
rand() & 0xff will return an 8-bit random number. If you're worried
about the quality of rand(), shifting it right a little helps in some
implementations.


Right of course.

Reading back the code makes me think: "How the [beeep] could I write
that...."
Nov 14 '05 #18

P: n/a
On Fri, 26 Nov 2004 08:50:03 +0100
"dandelion" <da*******@meadow.net> wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:v1*****************@newsread1.news.pas.earthl ink.net...


<snip>
Make sure you specify binary mode when calling 'fopen()'.


OP indicated UNIX as platform. Not DOS.


You should still use the correct mode since one day you might need to
port it to some other system.

In any case, what does it cost to use the correct mode which *will* work
rather than the incorrect mode and break it on other systems?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #19

P: n/a

"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:uk************@brenda.flash-gordon.me.uk...
On Fri, 26 Nov 2004 08:50:03 +0100
"dandelion" <da*******@meadow.net> wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:v1*****************@newsread1.news.pas.earthl ink.net...
<snip>
Make sure you specify binary mode when calling 'fopen()'.


OP indicated UNIX as platform. Not DOS.


You should still use the correct mode since one day you might need to
port it to some other system.


I was under the impression that "b" was a DOS-only thing. But some reading
has put me right. ANSI X3.159-1989 (``ANSI C'') does define it, it's just
ignored on most Un*x's.
In any case, what does it cost to use the correct mode which *will* work
rather than the incorrect mode and break it on other systems?


Not much...
Nov 14 '05 #20

P: n/a
On Mon, 29 Nov 2004 13:20:43 +0100, dandelion
<da*******@meadow.net> wrote:
I was under the impression that "b" was a DOS-only thing. But some reading
has put me right. ANSI X3.159-1989 (``ANSI C'') does define it, it's just
ignored on most Un*x's.


All Unices, as far as I am aware. The non-portable flag is "t" to force
text mode, it was introduced by (I think) Borland because they had
(have?) the ability to globally set the default mode so they wanted a
way to be able to force the mode both ways.

(The "b" flag is in the 99 standard as well, but not the "t" flag.)

Chris C
Nov 14 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.