creating empty file
-
How to create the the empty file with defined size in c++ or MFC. thanks for reply . see i am looking to create a file that should be empty and should not have data inside the file. you can say acquiring memory space from the particular drive .. Thanks
This is the easiest. Open/Create a new file and then close it immediately without writing into it. That's it.
-
This is the easiest. Open/Create a new file and then close it immediately without writing into it. That's it.
ya i agree what about the size which i want to define think that around 1gb or 100 mb .. thanks
-
ya i agree what about the size which i want to define think that around 1gb or 100 mb .. thanks
OK, it is time to decide: Do you want an empty file or one that is 1gb large???
-
OK, it is time to decide: Do you want an empty file or one that is 1gb large???
large file without any data inside the file . look what actually looking to do is to create a large file with define size.
-
large file without any data inside the file . look what actually looking to do is to create a large file with define size.
A file is either empty or large. It can not be both.
-
Hello , Is it possible to create a empty file with predefined size of that file. please reply me soon ... Regards Sarfaraz
No and yes. No, insofar as the file must contain some data. The data will be meaningless, but it will be there all the same. Yes, in that you can create a file that occupies 1GB without explictly writing 1gb of data to it. I write a single byte after settiing the file-pointer. This is enough to create the file with the desired size. It takes just as long to run for 1024 bytes as it does for 1024 megabytes. Code:
#include <stdio.h>
int main()
{
FILE *fp;
const int numMegabytes = 1024; // 1GB
int numBytes = (numMegabytes * 1<<20);fp = fopen("myFile.dat", "wb"); fseek(fp, numBytes-1, SEEK\_END); // -1 to account for extra byte we write fwrite("\\0", 1, 1, fp); // write a single NULL byte to ensure file-size gets set correctly. fclose(fp); return 0;
}
"Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin
-
Hello , Is it possible to create a empty file with predefined size of that file. please reply me soon ... Regards Sarfaraz
sarfaraznawaz wrote:
please reply me soon ...
Why, are you in a meeting and want to impress someone?
-
large file without any data inside the file . look what actually looking to do is to create a large file with define size.
-
large file without any data inside the file . look what actually looking to do is to create a large file with define size.
Read the documentation of SetFilePointerEx()[^], SetEndOfFile()[^] and SetFileValidData()[^] - these are the functions you need along with CreateFile()[^] and CloseHandle()[^]. Create a file with CreateFile(), set the file pointer with SetFilePointerEx() and then call SetEndOfFile(). As a last step close the file with CloseHandle(). This is how you preallocate a file on windows. Note that the preallocated file data will be overwritten with zeros by windows in order to prevent you from reading the previous contents of the files that were stored on these sectors before deletion/moving. If you want to ask windows to skip the overwrite with zeros (for example to save time because in case of big preallocation its time consuming) then you need a few privileges and a SetFileValidData() call. Read the function documentations and use google to find out how to do that trick.
-
No and yes. No, insofar as the file must contain some data. The data will be meaningless, but it will be there all the same. Yes, in that you can create a file that occupies 1GB without explictly writing 1gb of data to it. I write a single byte after settiing the file-pointer. This is enough to create the file with the desired size. It takes just as long to run for 1024 bytes as it does for 1024 megabytes. Code:
#include <stdio.h>
int main()
{
FILE *fp;
const int numMegabytes = 1024; // 1GB
int numBytes = (numMegabytes * 1<<20);fp = fopen("myFile.dat", "wb"); fseek(fp, numBytes-1, SEEK\_END); // -1 to account for extra byte we write fwrite("\\0", 1, 1, fp); // write a single NULL byte to ensure file-size gets set correctly. fclose(fp); return 0;
}
"Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin
The std solution. I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup. In DOS this trick created only a chain in the FAT without actually filling up the space so it was very quick. :-)
-
The std solution. I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup. In DOS this trick created only a chain in the FAT without actually filling up the space so it was very quick. :-)
pasztorpisti wrote:
I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup.
:laugh: I'm in the middle of playing with a similar piece of fun to throw at a friend. I'm planning to port HIDache[^] to the arduino platform with a 'software-only' implementation of the usb protocol[^] to send random keystrokes and mouse-strokes to his pc (or in fact, any device with a usb host that supports HID kbs and mice). Got plenty of old usb memory stick enclosures that will hold an Arduino ProMini. Looks like being the most fun $5 or $6 I've spent in a long time. :evil-grin: Can't even begin to imagine the fun if I manage to sneak an NRF24L01+ or A7105 2.4ghz transceiver module in there too!
"Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin
-
pasztorpisti wrote:
I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup.
:laugh: I'm in the middle of playing with a similar piece of fun to throw at a friend. I'm planning to port HIDache[^] to the arduino platform with a 'software-only' implementation of the usb protocol[^] to send random keystrokes and mouse-strokes to his pc (or in fact, any device with a usb host that supports HID kbs and mice). Got plenty of old usb memory stick enclosures that will hold an Arduino ProMini. Looks like being the most fun $5 or $6 I've spent in a long time. :evil-grin: Can't even begin to imagine the fun if I manage to sneak an NRF24L01+ or A7105 2.4ghz transceiver module in there too!
"Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin
:-) :-) :-)
-
Read the documentation of SetFilePointerEx()[^], SetEndOfFile()[^] and SetFileValidData()[^] - these are the functions you need along with CreateFile()[^] and CloseHandle()[^]. Create a file with CreateFile(), set the file pointer with SetFilePointerEx() and then call SetEndOfFile(). As a last step close the file with CloseHandle(). This is how you preallocate a file on windows. Note that the preallocated file data will be overwritten with zeros by windows in order to prevent you from reading the previous contents of the files that were stored on these sectors before deletion/moving. If you want to ask windows to skip the overwrite with zeros (for example to save time because in case of big preallocation its time consuming) then you need a few privileges and a SetFileValidData() call. Read the function documentations and use google to find out how to do that trick.
ok. i think its not possible to have empty file except filled with zeros... Thanks every body for your replies. Looking to make app that lock the usb data but am unable to start any idea or guidance will be appreciated . regards sarfaraz
-
large file without any data inside the file . look what actually looking to do is to create a large file with define size.
Hi, Yes, you can create 'empty' files of 1GB size. If you are on Microsoft Windows XP or above and using an NTFS file system then you can create a sparse file. This will allow you to create 'empty' files of any size that contain 'virtual zeros'. The Windows operating system will report the file as having a valid file size... and it will even apply to user disk quota. Sparse Files[^] Steps to create a sparse file: 1.) Call CreateFile [^]and create a new file. Keep the file handle open. 2.) Call DeviceIoControl [^] on your open file handle with the FSCTL_SET_SPARSE control code[^] to mark the file as sparse. 3.) Call the SetFilePointerEx function[^] to move from
FILE_BEGIN
past the end of file. Make sure to assignliDistanceToMove
argument to the desired file size. 4.) Call the SetEndOfFile function[^] to set the EOF to the current position of the file pointer. 5.) Close your file hande. Best Wishes, -David Delaune -
sarfaraznawaz wrote:
please reply me soon ...
Why, are you in a meeting and want to impress someone?
I know you're helpful here normally, but that's not really an answer, Eric. :)
"Real men drive manual transmission" - Rajesh.
-
I know you're helpful here normally, but that's not really an answer, Eric. :)
"Real men drive manual transmission" - Rajesh.
If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)
-
If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)
Can we create the virtual drive of usb .
-
If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)
Erudite_Eric wrote:
If a post offends me then I will reply in an appropriate manner. :)
Yeah, I've done that before, but I found that to be of useful to nobody. I thought I'd just point it out, but you're free to do what you feel is right.
Erudite_Eric wrote:
By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person.
Now, you're being offensive. Because I consider myself as a regggular here. :) Anyone who's been here long enough (and regular) will know your story. But it has nothing to do with the icebergs melting, I tell you.
"Real men drive manual transmission" - Rajesh.