读取一个txt文件的内容写到缓冲区,并求得缓冲区的大小,最好能给出实例,谢谢!

解决方案 »

  1.   

    CString sText;
    CFile file("c:\\test.txt", CFile::modeRead);
    DWORD dwLen = file.GetLength();
    file.Read(sText.GetBuffer(dwLen), dwLen);
    AfxMessageBox(sText);
      

  2.   

    不过我想用FILE*方法,用fread函数怎么取啊,如果是一个很大的txt文件,把它读出来写到另一个txt文件中?是不是要分段读取啊
      

  3.   

    Example/* FEOF.C: This program uses feof to indicate when
     * it reaches the end of the file FEOF.C. It also
     * checks for errors with ferror.
     */#include <stdio.h>
    #include <stdlib.h>void main( void )
    {
       int  count, total = 0;
       char buffer[100];
       FILE *stream;   if( (stream = fopen( "feof.c", "r" )) == NULL )
          exit( 1 );   /* Cycle until end of file reached: */
       while( !feof( stream ) )
       {
          /* Attempt to read in 10 bytes: */
          count = fread( buffer, sizeof( char ), 100, stream );
          if( ferror( stream ) )      {
             perror( "Read error" );
             break;
          }      /* Total up actual bytes read */
          total += count;
       }
       printf( "Number of bytes read = %d\n", total );
       fclose( stream );
    }
    OutputNumber of bytes read = 745
      

  4.   

    FILE *fp;
    if(!(fp=fopen("C:\\Roy.txt","rb")))
    return; if(fseek(fp,0l,SEEK_SET))
    {
    fclose(fp);
    return;
    } DWORD fcount;
    char buf[4096];
    memset(buf, 0, 4096);
    while((fcount=fread(buf,sizeof(char),4096,fp)))
    {
    //如果你要向另一个文件写入,只要在这里写入fcount长度就可以了
    MessageBox(buf);   
    }
    fclose(fp);