Einzelnen Beitrag anzeigen

Muetze1
(Gast)

n/a Beiträge
 
#4

Re: ein struct mit fwrite in Datei schreiben

  Alt 8. Jan 2007, 21:43
Zitat von PAX:
hey, der mütze?
Welcher denn? Oder wie?

So, eben nochmal ausprobiert und klappt bei mir. Folgender Code und Deklarationen:

Code:
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  FILE *fp = NULL;
  FileStruct maske;

  fp = fopen(ChangeFileExt(Application->ExeName, ".tst").c_str(), "wb");

  if (fp)
  {
    maske.a = StrToInt(Edit1->Text);
    maske.b = StrToInt(Edit2->Text);
    maske.c = StrToInt(Edit3->Text);
    maske.d = StrToInt(Edit4->Text);

    if (fwrite(&maske, sizeof(maske), 1, fp) != 1)
      ShowMessage("file not correctly written!");

    fclose(fp);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  FILE *fp = NULL;
  FileStruct maske;

  fp = fopen(ChangeFileExt(Application->ExeName, ".tst").c_str(), "rb");

  if (fp)
  {
    if (fread(&maske, sizeof(maske), 1, fp) != 1)
      ShowMessage("file not correctly read!");

    fclose(fp);

    Label1->Caption = IntToStr(maske.a);
    Label2->Caption = IntToStr(maske.b);
    Label3->Caption = IntToStr(maske.c);
    Label4->Caption = IntToStr(maske.d);
  }
}
//---------------------------------------------------------------------------
Die Struktur:

Code:
struct FileStruct {
  int a, b, c, d;
};
  Mit Zitat antworten Zitat