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

simple file system

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
20 5109


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

"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
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
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
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
"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
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
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

"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
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
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

"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

"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

"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

"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
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

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aamir | last post by:
Hi, Quick Question: How to create a simple XML with US States list and read it into a combo box. Detail: I am very new to Dot Net technology as well as xml. I am building a desktop app in...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
0
by: Ming Zhu | last post by:
Hi, all, I'm new to .NET and ASP.NET. I only use .NET Framework 1.0. I'm reading the book "C# developer's guide to ASP.NET". But I find there is no place to download the code used inside this...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
3
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { //...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
1
by: lucifer | last post by:
hi, i am creating an simple http server ie it serves only static pages . u can compile the code then use ur browser if it is IE then the it shows the page but the http header is also shown...
2
by: lucifer | last post by:
hi, i am creating an simple http server ie it serves only static pages . u can compile the code then use ur browser if it is IE then the it shows the page but the http header is also shown...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
0
by: myself337 | last post by:
in VB you can do this: Imports System Imports System.IO Module Module1 Sub Main() Dim path As String = "c:\temp\filetocopy.txt" ' put the file you
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...

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.