[BCB(C++ Builder)/C/C++] 檔案 讀取
[BCB(C++ Builder)/C/C++] 檔案 讀取
資料來源: https://sites.google.com/site/sjdsalg/materials/program/readwritefiles
code [Read]
#include "stdio.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- int maze [20][20]; void __fastcall TForm1::Button1Click(TObject *Sender) { FILE *fp; AnsiString out; int m, n, i, j; if (OpenDialog1->Execute()) { fp = fopen(OpenDialog1->FileName.c_str(), "r+"); //c_str函数的傳回值是 const char *, 即把string轉成const char * fscanf(fp, "%d %d", &m, &n); // Reda in two integers m & n Memo1->Lines->Add("m="+IntToStr(m)); Memo1->Lines->Add("n="+IntToStr(n)); for (i=1; i<=m; i++) // Reda in m*n 0/1's into maze[][] for (j=1; j<=n; j++) fscanf(fp, "%d", &maze[i][j]); for (i=1; i<=m; i++) // Print out maze[][] in Memo1 { out = ""; for (j=1; j<=n; j++) out += " "+IntToStr(maze[i][j]); Memo1->Lines->Add(out); } // Print out maze[][] in StringGrid1 StringGrid1->RowCount = m+1; StringGrid1->ColCount = n+1; for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { StringGrid1->Cells[j][i] = maze[i][j]; } } fclose(fp); } else Memo1->Lines->Add("Nothing happens."); }