// 以下是在 C++ Builder 撰寫的範例
// "in.txt" 為 .txt 檔案,且須事先將 in.txt 檔案放在與 .exe 檔案相同的目錄,以下為 in.txt 檔內容:
// 5 6
// 0 0 0 0 0 0
// 0 1 1 1 1 0
// 0 1 1 1 1 0
// 0 1 1 1 1 0
// 0 0 0 0 0 0
#include <stdio.h>
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std; // Add this statement for using "cin"/"cout"
TForm1 *Form1;
//----------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
}
//------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ int m, n, i, j;
freopen("in.txt", "r", stdin);
// read data in "in.txt" as standard input 將 in.txt 內容當成 stdin
freopen("out.txt", "w", stdout); // 將輸出結果寫入 "out.txt" 檔案,會在與 .exe 相同之目錄生成 out.txt 檔案
cin >> m >> n; // 自 in.txt (stdin) 輸入矩陣大小
int ** maze;
maze = new int * [m];
for (int i=0; i<m; i++)
{ maze[i] = new int [n];
for (int j=0; j<n; j++)
cin >> maze[i][j]; // 自 in.txt (stdin) 讀資料
}
fclose(stdin);
cin.clear();
String s; // 將輸出寫入 Form1
for (int i=0; i<m; i++)
{ for (int j=0, s=""; j<n; j++)
s = s + IntToStr(maze[i][j]) + " ";
Form1->Memo1->Lines->Add(s);
}
// 將輸出寫入 out.txt 檔案
cout << m << " " << n << endl;
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
cout << maze[i][j] <<" ";
cout << endl;
}
fclose(stdout);
}
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
將檔案內容(含兩整數m,n和 m*n 個整數)讀入程式中 (BCB):
#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, fname;
int m, n, i, j;
if (OpenDialog1->Execute())
{ fname = OpenDialog1->FileName;
fp = fopen(fname.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-1][i-1] = maze[i][j];
}
}
fclose(fp);
}
else Memo1->Lines->Add("Nothing happens.");
}
--------------
4 4
0 1 1 0
0 0 1 1
1 0 0 1
1 1 1 0
--------------
將檔案內容當整數讀入程式中 (VisualStudio C++):
# include <stdio.h>
(or # include <iostream>)
// 欲使用檔案功能, 請 include stdio.h 或 iostream 擇一即可
char using namespace System::Runtime::InteropServices;
// 呼叫 Marshal::StringToHGlobalAnsi 轉換 String ^ to char * 時用!
// 下面的 fopen 需要檔名以 char * 型態為參數
String ^ a;
String ^ b;
int m, p;
char * fname;
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) // 順利開啟檔案才繼續往下做
{ a = openFileDialog1->FileName;
char * str2 = (char*) (void*) Marshal::StringToHGlobalAnsi(a);
FILE *fp = fopen(str2,"r"); // 讀檔, 注意第二參數為 "r" (read)
if (fp)
{ fscanf(fp, "%d %d", &m, &p);
listBox1->Items->Add("m="+m+" p="+p);
listBox1->Items->Add(a);
fclose(fp); // 讀完檔後請關檔
}
// 下面程式碼將檔名 xxx.yyy 變更為 xxx-1.yyy 做為寫入檔案的檔名
int k = a->IndexOf(".");
b = a->Substring(0, k)+"-1"+a->Substring(k, a->Length-k);
str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(b);
listBox1->Items->Add(b);
fp = fopen(str2,"w"); // 寫檔, 注意第二參數為 "w" (write)
if (fp)
{ fprintf(fp, "m=%d p=%d", m, p);
putc('\n', fp);
fclose(fp); // 寫完檔後務必關檔
}
fp = fopen(str2,"a"); // 自檔尾寫入, 注意第二參數為 "a" (append)
if (fp)
{ fprintf(fp, "append=> m=%d p=%d", m, p);
fclose(fp); // 寫完檔後務必關檔
}
}
-------------------------------------------------------------------------------------------
將檔案內容當串流(stream)讀讀入程式中 (VisualStudio C++):
using namespace System::IO;
using namespace System::Text;
// 呼叫StreamReader與StreamWriter用
if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) // 順利開啟檔案才繼續往下做
{
// 讀檔
StreamReader^ file = gcnew StreamReader(openFileDialog1->FileName);
String^ one_line = file->ReadLine(); // 一次讀檔案中的一列
cli::array<String^>^ split = one_line->Split(' ');
// 以空白當作分隔符號抓取每兩個空白間的字串
/*
例如:
檔案中的第一列串流是 Data Structures and Algorithms
我們以空白字元當分隔符號去分隔第一列串流
就能分別擷取出Data, Structures, and, Algorithms這四個字串
*/
// 寫檔
StreamWriter^ output = gcnew StreamWriter(Directory::GetCurrentDirectory()+" - output.txt");
// 必須給系統知道寫檔的檔名與存放路徑
for(i=0; i< dataGridView1->RowCount; i++)
{
String^ forOutput = "";
for(j=0; j<dataGridView1->ColumnCount; j++)
{
forOutput += dataGridView1->Rows[i]->Cells[j]->Value;
}
output->WriteLine(forOutput); // 一次寫出一列,寫完後即換行
}
output->Close(); // 寫完檔後務必關檔
}