同题目,我想生成16进制文件,生成这个文件可以直接用16进制编辑器打开,这个文件中比如offset都应该由程序指定。如果c#实现不了,需要用什么实现?最好给代码演示,多谢高手!

解决方案 »

  1.   

    FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
    BinaryWriter bw = new BinaryWriter(fs);bw.Write(......)  随便什么基本类型都能写
      

  2.   

    >>offset都应该由程序指定
    可以用
    FileStream.Position
    StreamWriter.BaseStream.Position
    StreamReader.BaseStream.Position
    ……
    来指定offset
    他们都有一个SetLength方法可以修改文件长度
      

  3.   

    是的,16进制文件,我想用程序 可以操作16进制文件,可以 决定 某一string 在16进制中的 offset ;可以 按offset来取得这个string
      

  4.   

    用fseekMSDN中的示例:
    Example
    // crt_fseek.c
    /* This program opens the file FSEEK.OUT and
     * moves the pointer to the file's beginning.
     */#include <stdio.h>int main( void )
    {
       FILE *stream;
       char line[81];
       int  result;   stream = fopen( "fseek.out", "w+" );
       if( stream == NULL )
          printf( "The file fseek.out was not opened\n" );
       else
       {
          fprintf( stream, "The fseek begins here: "
                           "This is the file 'fseek.out'.\n" );
          result = fseek( stream, 23L, SEEK_SET);
          if( result )
             perror( "Fseek failed" );
          else
          {
             printf( "File pointer is set to middle of first line.\n" );
             fgets( line, 80, stream );
             printf( "%s", line );      }
          fclose( stream );
       }
    }
    Output
    File pointer is set to middle of first line.
    This is the file 'fseek.out'.
      

  5.   

    int a = 15;
    Console.WriteLine( a.ToString( "{X}" ) );
    可以给你这么点提示。