0

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();
 }
U chaudrhy
  • 401
  • 1
  • 5
  • 16

3 Answers3

4

You are filling it one character at a time. Instead, you could allocate a larger chunk of memory using new and then write larger chunks at once to speed up the process. You could use memset on the allocated memory to prevent having bytes characters in the memory. But also look at the comment about the duplicate question, there are even faster methods if the file needn't have specific content initially.

fpw
  • 801
  • 4
  • 12
  • That is rather inefficient. – Domi Nov 28 '13 at 13:12
  • I assumed the asker wants the file to be filled with spaces initially and not just create the file. Updated the answer to clarify that. – fpw Nov 28 '13 at 13:14
  • I did see the duplicate question, however when i use those methods, using a buffer of size 1024 and using fputs to write it to file, the files gets created faster however I also get this stupid string written to the file "Íýýýý««««««««îþîþ" along with the spaces. How should I remove that? – U chaudrhy Nov 28 '13 at 13:32
  • As stated above: Use `memset` on the allocated memory to set the buffer to ' '. – fpw Nov 28 '13 at 13:33
  • I used memset but this 'Íýýýý««««««««îþîþ' string is still there – U chaudrhy Nov 28 '13 at 13:43
  • Update the code in your question so we can see it. – fpw Nov 28 '13 at 14:00
  • @user1880637: Read the documentation for the functions that you use. `fputs` writes a string, a null-terminated string, into a file. Your buffer's _first byte_ is a NULL, so it does nothing. – Lightness Races in Orbit Nov 28 '13 at 14:35
0

Here a simple sample, but without error checking.

Lets say you want size of 1000:

#include <fstream>   

int main () {
  int size = 1000;
  const char* filename= "file.txt";
  std::ofstream fout(filename);

  fout.fill (' ');
  fout.width (size);
  fout << " ";

  return 0;
}
SHR
  • 7,940
  • 9
  • 38
  • 57
0

You don't have to fill all of bytes in file if you just want to create a big file, that make it slow, just tell the file-system how big you want

On Linux, use truncate

truncate("data.txt",1024*1024*1024);

Windows use SetFilePointer

SetFilePointer (hFile, 1024*1024*1024, NULL, FILE_BEGIN); 

both of them can create uninitialized file of some Gigabytes in less than seconds.

secmask
  • 7,649
  • 5
  • 35
  • 52