473,546 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create new file with fixed size

Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(str ing fileName, long
initialSizeInBy tes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks

Aug 31 '07 #1
4 16647
On 31 , 12:59, nano2k <adrian.rot...@ ikonsoft.rowrot e:
Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(str ing fileName, long
initialSizeInBy tes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks
System.IO.File. WriteAllBytes(" file.txt", new byte[100]);

Regards,
Mykola
http://marss.co.ua

Aug 31 '07 #2
On 31 Aug, 13:09, marss <marss...@gmail .comwrote:
On 31 , 12:59, nano2k <adrian.rot...@ ikonsoft.rowrot e:
Hi
For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(str ing fileName, long
initialSizeInBy tes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?
Thanks

System.IO.File. WriteAllBytes(" file.txt", new byte[100]);

Regards,
Mykolahttp://marss.co.ua
Hi Mykola
Thanks for your response.
I'm using .NET Framework 1.1, so this method you suggested is not
available in my case.
The files I will create are far larger than you suggested in your
post. May be hundreds of megs, or even few gigs.
What you suggest is that I should create a file, than write the amount
of bytes I need. Writing in one shot is not ok. It's not my desire to
allocate a huge buffer just to tranfer it to a file.
I'd rather write to file successively a reasonable sized buffer until
the desired size is reached.
This is my approach at the moment, but I thought that there should be
a another, more optimum, way to do it.

Aug 31 '07 #3
Hello nano2k,

use this one
using (FileStream stream = new FileStream(path , FileMode.Create , FileAccess.Writ e,
FileShare.Read) )
{
stream.Write(by tes, 0, bytes.Length);
}

u can do in blocks, writing to file several times

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
nOn 31 Aug, 13:09, marss <marss...@gmail .comwrote:
n>
>On 31 , 12:59, nano2k <adrian.rot...@ ikonsoft.rowrot e:
>>Hi

For some purposes, I need to create a new binary file and to
allocate
a fixed length for that file on the disk.
I need something like File.Create(str ing fileName, long
initialSizeIn Bytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?
Thanks
System.IO.File .WriteAllBytes( "file.txt", new byte[100]);

Regards,
Mykolahttp://marss.co.ua
nHi Mykola
nThanks for your response.
nI'm using .NET Framework 1.1, so this method you suggested is not
navailable in my case.
nThe files I will create are far larger than you suggested in your
npost. May be hundreds of megs, or even few gigs.
nWhat you suggest is that I should create a file, than write the
namount
nof bytes I need. Writing in one shot is not ok. It's not my desire to
nallocate a huge buffer just to tranfer it to a file.
nI'd rather write to file successively a reasonable sized buffer until
nthe desired size is reached.
nThis is my approach at the moment, but I thought that there should be
na another, more optimum, way to do it.
Aug 31 '07 #4
On Fri, 31 Aug 2007 02:59:18 -0700, nano2k <ad***********@ ikonsoft.ro>
wrote:
>Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(str ing fileName, long
initialSizeInB ytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks


FileStream fs=new FileStream("??? ", FileMode.Create );
long len=....
fs.SetLength(le n);

Note that the contents of the file are *not* guranteed to be 0.

Austin
Aug 31 '07 #5

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

Similar topics

10
5464
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the following: struct Node { int integer_value;
11
6720
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the same type). eg: int arr1;//Array of 20 ints int arr2; int arr3; ............
2
4641
by: ldawson | last post by:
From the same C++ source code, I'm attempting to generate both an unmanaged DLL and a managed assembly. This will eliminate interop as the calling code is slowly migrated to .NET. However, C++ native compilation allows fixed size buffers (e.g. char pathName) which we've used to represent a memory mapped file, and I can't find out how to...
0
1515
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of redundancy and extra code to have to marshal .net data from managed to unmanaged for this. Is there some built-in method in managed C++ that can...
5
4157
by: collection60 | last post by:
Hi people, I am writing a library that will implement a binary file format. Well... I'm thinking, as a design, it would be nice to have my library able to promise to not use more than a certain amount of RAM. I could have a "SetCacheSize(long NewSize);" function. So as part of that promise, when required memory exceeds that limit, the...
8
3711
by: chrisben | last post by:
Hi, If I am not sure how many items will be in my Hashtable/ArrayList dynamically (multiple threads can add/remove/check the item), however, I can estimate the total number will not exceed 60000. How much can I gain in term of performance if I decalre the hash as Hashtable h = new Hashtable(60000); instead of
2
4053
by: neuneudr | last post by:
Hi everybody, I'm scratching my head with a CSS problem. I want to have the following (the two pics have these size for a good reason, the whole point of the page is basically to show these pices at these fixed sizes) : spacer - fixed size picture - spacer - fixed size picture - spacer
1
2754
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define the fixed size using In the attached console application example when I try to address the fields in the class I get an execption.
2
6602
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have to create a buffer of a pre-defined length: (ConstBufferByteSize=10000000). How can I read a stream into a buffer or string without knowing the...
1
4121
by: Man4ish | last post by:
I am trying to create a file of fixed size. I am using setw() e.g. 43 68 + 169161408 115 287 + 169161408 I am using setw() after first column cout<<col1<<setw(20)<<col2<<setw(20)<<col3<<setw(20)<<col4<<endl; But still all records are not of fixed length I have calculated the length but in some cases it is coming different.
0
7698
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. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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...
0
6030
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...
1
5361
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...
0
5080
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.