在一个比较大的文件头部添加数据,请问怎么实现?
文件大小上M字节。
要求:效率最高。
欢迎大家来讨论!

解决方案 »

  1.   

    先把要加的数据以二进制写到文件里
    然后用dos的copy命令把两个文件接起来
      

  2.   

    CStringFile Class - Frank Driesens (2000/04/02)
    Efficient means of reading large text files CStringFile Class --------------------------------------------------------------------------------This article was contributed by Frank Driesens.Environment: VC5/6, NT4, CE 2.11Once upon a day I was asked to write an application which could do some serious filtering, on a ';' seperatedfile which held multiple collumns. The initial file was about 7 Mb in size, so I had to create a program which could read a file, process the read lines and produce a new output file.
    So you think, what's the big deal ?
    When I wrote that program, a pentium 133 was considered a fast PC. As far as I knew, there where no generic (Microsoft provided ??) solutions for a simple task as reading text from a file. So I build my own textfile reading class.
    This brings me to the part which I found most interesting, the first version of this filtering program, did the job in several seconds.
    So how come the thing worked so fast ? By the optimal manner of reading a text file.The StringFile class itself consists of 2 loops. One loop is for filling a read buffer, and the other loop is used for reading a line from this buffer. The effect of these loops is that when the file is read, its is done by reading 2k of data per turn. Further processing (finding where any given line starts and when it stops) is done inside memory, not on disk. And, as you can probably known, memory is faster then disk...so there's my explanation for the speed of the filter program.
    After some fiddling around with this class I decided to post this, so that everyone can enjoy this piece of code. This sample show just how easy this thing works, open, read and close. What do you want more ?? #include "StringFile.h"BOOL ReadTextFile(LPCSTR szFile)
    {
     CStringFile  sfText;
     CString szLine;
     BOOL bReturn = FALSE;  // When the given file can be opened
     if(sfText.Open(szFile)) 
     {
      // Read all the lines (one by one)
      while(sfText.GetNextLine(szLine)!=0)
      {
       printf("%s\r\n",szLine); //And print them
      }
      sfText.Close(); // Close the opened file
      bReturn = TRUE; // And say where done succesfully
     }
     return bReturn;
    }Some benching (trying to find optimum blocksize for reading) gave me the following results:
    This shows that the optimum size for this piece of code lies around 2k blocksize. Increasing this blocksize doesn't speedup reading, the only thing speeding up the read actions is probably improving the used code.Downloads
    Download source - 3 Kb 
    History
    Date Posted: April 2, 2000http://www.codeguru.com/files/StringFile.zip
      

  3.   

    先把旧文件头割离。
    新建一个文件,写入头,再把用xcopy考到一起。
      

  4.   

    http://www.vckbase.com/document/viewdoc.asp?id=293
      

  5.   

    取消上面那个,看这个
    http://codeguru.earthweb.com/tools/QuickSplit.shtml
      

  6.   

    谢谢楼上的各位
    一个朋友说了个办法,觉得不错,与大家共享
    先创建一个新文件,写入要添加的数据,
    然后将此文件映射CreateFileMapping,其第四个参数dwMaximumSizeLow设为此文件大小加要添加的文件大小,然后将要添加的文件也映射入内存,起始地址为刚创建的文件尾,用MapViewOfFileEx可实现。
    这样,两个文件就衔接起来了,只要设置一下前一个文件指针即可,SetEndOfFile
    然后UnMapViewOfFile就可以了。