String a="sadfsad";
fstream file;
file.open("data.loo",ios::out|ios::binary);
file.write((char *)&a,sizeof(String));
................以上是反面教材,结果是不可读取a的,如何才能储存a????各位大师多多指教!!

解决方案 »

  1.   

    char a[100] = "hello world";
    fstream file;
    file.open("data.loo",ios::out|ios::binary);
    file.write(a,sizeof(a));你这个是什么语言那FStream吧,C或C#都区分大小写,VC是String
      

  2.   

    给个例子给你static String FileName = "c:\\temp.dat";void WriteMsg(void* Buffer, int Count)
    {
      Word Mode = fmCreate;
      if (FileExists(FileName))
        Mode = fmOpenWrite;
      TFileStream* Stream = new TFileStream(FileName, Mode);
      try
      {
        Stream->Seek(0, (short)soEnd);
        //or Stream->Position = Stream->Size;
        // WriteBuffer和Write的区别就是: WriteBuffer会检查写入的数量是否是Count
        // 如果不等于,则抛出一个异常
        // 相当于:
        // if (Write(Buffer, Count) != Count)
        //  throw new Exception("Stream Write error");
        Stream->WriteBuffer(Buffer, Count);
      }
      __finally
      {
        delete Stream;
      }
    }void ReadMsg(void* Buffer, int Count, int Offset = 0)
    {
      if (!FileExists(FileName))
        throw new Exception("file not exists");
      TFileStream* Stream = new TFileStream(FileName, fmOpenRead);
      try
      {
        Stream->Seek(Offset, (short)soCurrent);
        Stream->ReadBuffer(Buffer, Count);
      }
      __finally
      {
        delete Stream;
      }
    }int GetFileSize()
    {
      if (!FileExists(FileName))
        throw new Exception("File not exists");
      int hFile = FileOpen(FileName, (unsigned)fmOpenRead);
      try
      {
        return FileSeek(hFile, 0, (int)soEnd);
      }
      __finally
      {
        FileClose(hFile);
      }
    }void __fastcall TForm1::Button1Click(TObject *Sender)
    {
      String S = "hello world";
      WriteMsg(S.c_str(), S.Length());  int i = 10;
      WriteMsg(&i, sizeof(i));  BYTE b = 10;
      WriteMsg(&b, sizeof(b));  BYTE B[10] = {1, 2, 3, 4, 5};
      WriteMsg(B, sizeof(B));  char c[100] = "this is array c";
      WriteMsg(c, sizeof(c));  int Size = GetFileSize();  S.SetLength(Size);
      ReadMsg(S.c_str(), Size);
      ShowMessage(S);  void* Buffer = malloc(Size);
      void* p = Buffer;
      try
      {
        BYTE Value;
        ReadMsg(Buffer, Size);
        for(i = 0; i < Size; i++)
        {
          Value = *((BYTE*)p)++;
          Memo1->Lines->Add(Format("Offset: %d Value: %d\n", ARRAYOFCONST((i, Value))));
        }
      }
      __finally
      {
        free(Buffer);
      }
    }
      

  3.   

    不会BCB,试试着改了一点看行不行:
    String a="sadfsad";
    fstream file;
    file.open("data.loo",ios::out|ios::binary);
    file.write((char *)&a[1],Length(String));
      

  4.   

    ok,现在能够储存String了,但如何储存一个struct里的String呢?for example:
      struct pro
      {
       int a;
       TPoint b;
       String c;
      };now how to save this struct???thank you for your recive!!
      

  5.   

    typedef struct pro
      {
       int a;
       TPoint b;
       char c[50];
      };pro Value;
    Value.a = 10;
    Value.b.x = 20;
    Value.b.y = 30;
    strcpy(Value.c, "hello world");WriteMsg(&Value, sizeof(Value));在struct中不要使用没有分配字节的数据类型,如String, char c[]这种类型,否则写数据时将不能正确写入数据。