Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: creating empty file Pin
pasztorpisti30-Jul-13 0:12
pasztorpisti30-Jul-13 0:12 
GeneralRe: creating empty file Pin
sarfaraznawaz30-Jul-13 0:20
sarfaraznawaz30-Jul-13 0:20 
GeneralRe: creating empty file Pin
pasztorpisti30-Jul-13 0:50
pasztorpisti30-Jul-13 0:50 
GeneralRe: creating empty file Pin
jeron130-Jul-13 5:06
jeron130-Jul-13 5:06 
GeneralRe: creating empty file Pin
pasztorpisti30-Jul-13 5:43
pasztorpisti30-Jul-13 5:43 
GeneralRe: creating empty file Pin
sarfaraznawaz30-Jul-13 21:24
sarfaraznawaz30-Jul-13 21:24 
AnswerRe: creating empty file Pin
Randor 30-Jul-13 21:39
professional Randor 30-Jul-13 21:39 
AnswerRe: creating empty file Pin
enhzflep30-Jul-13 2:05
enhzflep30-Jul-13 2:05 
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:
C++
#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

GeneralRe: creating empty file Pin
pasztorpisti30-Jul-13 6:21
pasztorpisti30-Jul-13 6:21 
GeneralRe: creating empty file Pin
enhzflep30-Jul-13 6:46
enhzflep30-Jul-13 6:46 
GeneralRe: creating empty file Pin
pasztorpisti30-Jul-13 7:00
pasztorpisti30-Jul-13 7:00 
AnswerRe: creating empty file Pin
Erudite_Eric30-Jul-13 2:05
Erudite_Eric30-Jul-13 2:05 
GeneralRe: creating empty file Pin
Rajesh R Subramanian31-Jul-13 20:51
professionalRajesh R Subramanian31-Jul-13 20:51 
GeneralRe: creating empty file Pin
Erudite_Eric31-Jul-13 22:01
Erudite_Eric31-Jul-13 22:01 
GeneralRe: creating empty file Pin
sarfaraznawaz1-Aug-13 2:51
sarfaraznawaz1-Aug-13 2:51 
GeneralRe: creating empty file Pin
Rajesh R Subramanian1-Aug-13 6:07
professionalRajesh R Subramanian1-Aug-13 6:07 
QuestionHow to improve quality of a video.? Pin
mbatra3129-Jul-13 20:35
mbatra3129-Jul-13 20:35 
AnswerRe: How to improve quality of a video.? Pin
Erudite_Eric29-Jul-13 21:24
Erudite_Eric29-Jul-13 21:24 
GeneralRe: How to improve quality of a video.? Pin
mbatra3129-Jul-13 21:35
mbatra3129-Jul-13 21:35 
GeneralRe: How to improve quality of a video.? Pin
Erudite_Eric30-Jul-13 0:20
Erudite_Eric30-Jul-13 0:20 
GeneralRe: How to improve quality of a video.? Pin
mbatra3130-Jul-13 0:50
mbatra3130-Jul-13 0:50 
GeneralRe: How to improve quality of a video.? Pin
Erudite_Eric30-Jul-13 2:08
Erudite_Eric30-Jul-13 2:08 
GeneralRe: How to improve quality of a video.? Pin
mbatra3130-Jul-13 2:15
mbatra3130-Jul-13 2:15 
GeneralRe: How to improve quality of a video.? Pin
mbatra3130-Jul-13 2:19
mbatra3130-Jul-13 2:19 
GeneralRe: How to improve quality of a video.? Pin
Erudite_Eric30-Jul-13 2:45
Erudite_Eric30-Jul-13 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.