I have to create a text file of specific size, user enters the size. All I need to know is how to make a file faster. Currently creating a 10mb file takes about 15 seconds. I have to decrease this to max 5 seconds. How can I do that? Currently this is how I am making my file
void create_file()
{
int size;
cout<<"Enter size of File in MB's : ";
cin>>file_size;
size = 1024*1024*file_size; // 1MB = 1024 * 1024 bytes
ofstream pFILE("my_file.txt", ios::out);
for(int i=0; i<size; i++) //outputting spces to create file
pFILE<<' ';
pFILE.close();
}
Update, this is what I am using now, but I get garbage value written to the file as well,
void f_c()
{
int i, size;
cin>>size;
FILE * new_file = fopen("FILE TEST.txt", "w");
char buffer[1024];
memset(buffer,' ', 1024);
for(i = 0; i<1024 * size; i++)
fputs(buffer, new_file);
getchar();
}