6. 特殊功能的處理
6.1
檔案處理
(1). 以 FILE 的方式處理文字檔 (FILE)
Open / Close FILE *fp;
fp = fopen("filename","mode");
...
fclose(*fp);
¾ 本作業在取得檔案的 Pointer, 以便進一步操作。
¾ filename 則需包括檔案的 path。
¾ mode
“r" : Read only
“w" : Write
“a" : Append at the end of the file + : Read & Write
¾ fclose 可結束作業, 並將結果寫入。
Write ...
char *str1 = “test1”;
char *str2 = “test2”
int t1 = 12;
fprintf(fpt,"%s %s %d",str1,str2,t1);
...
¾ 本作業在寫入資料到檔案中。
¾ fpt: File Pointer
¾ “%s %s %d" 為所要寫入的格式。
Read ...
char *str1 = new char;
char *str2 = new char;
int t1;
fscanf(fpt,"%s %s %d",str1,str2,&i1);
...
¾ 本作業在從檔案中讀出資料。
¾ 所讀的資料會寫到右邊的變數中。
¾ 所讀出的資料並不會自動換行, 區隔變數的依據是'空白'。
下列何者為開檔的指令? (1) fclose (2) fopen (3) fprintf (4) fscanf Ans: (2)
下列何者為寫入檔案的指令? (1) fclose (2) fopen (3) fprintf (4) fscanf Ans: (3)
範例: (Write)
下面程式執行後,test1.txt 的內容為何?
FILE *pFile1;
char str1[10] = "test1";
int t1 = 12;
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s %d",str1,t1);
fclose(pFile1);
Ans: test1 12
範例: (Append)
下面程式執行後,test1.txt 的內容為何?
FILE *pFile1;
char str1[10] = "test1";
char str2[10] = "test2";
int t1 = 12;
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str1);
fclose(pFile1);
pFile1 = fopen("test1.txt","a");
fprintf(pFile1,"%s \n",str2);
fclose(pFile1);
Ans: test1 test2
範例: (Append)
下面程式執行後,test1.txt 的內容為何?
FILE *pFile1;
char str1[10] = "test1";
char str2[10] = "test2";
int t1 = 12;
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str1);
fclose(pFile1);
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str2);
fclose(pFile1);
pFile1 = fopen("test1.txt","a");
fprintf(pFile1,"%d \n",t1);
fclose(pFile1);
Ans: test2
12
範例: (Read)
下面程式執行後結果會印出甚麼?
FILE *pFile1;
char str1[10] = "test1";
char str2[10] = "test2";
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str1);
fclose(pFile1);
pFile1 = fopen("test1.txt","a");
fprintf(pFile1,"%s \n",str2);
fclose(pFile1);
pFile1 = fopen("test1.txt","r");
fscanf(pFile1,"%s",str1);
printf("%s\n", str1);
fclose(pFile1);
Ans: test1
範例: (Read)
下面程式執行後結果會印出甚麼?
FILE *pFile1;
char str1[10] = "test1";
char str2[10] = "test2";
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str1);
fclose(pFile1);
pFile1 = fopen("test1.txt","a");
fprintf(pFile1,"%s \n",str2);
fclose(pFile1);
pFile1 = fopen("test1.txt","r");
fscanf(pFile1,"%s",str1);
fscanf(pFile1,"%s",str1);
printf("%s\n", str1);
fclose(pFile1);
Ans: test2
範例: (Read)
下面程式執行後結果會印出甚麼?
FILE *pFile1;
char str1[40] = " test11 test12 12 13 test21 test22 22 14";
char str2[40];
int t1 = 0;
pFile1 = fopen("test1.txt","w");
fprintf(pFile1,"%s \n",str1);
fclose(pFile1);
pFile1 = fopen("test1.txt","r");
fscanf(pFile1,"%s %s %d",str1,str2,&t1);
printf("%s %s %d ", str1, str2, t1);
fscanf(pFile1,"%s %s %d",str1,str2,&t1);
printf("%s %s %d ", str1, str2, t1);
fclose(pFile1);
Ans: test11 test12 12 13 test21 12 (t1 並沒有更改,且最後的 22 & 14 不會印出)
練習 Example: File I/O (ex. file_io)
寫一個程式, 可讓使用者輸入基本資料。
提供一個存檔的供能。
可針對其中某些紀錄讀取修改並回存。
(2). 以 ifstream 的方式處理文字檔 (FILE)
Open / Close
#include<fstream>
#include<string.h>
fstream iFile;
iFile.open("filename.txt");
…
iFile.close();
¾ 要引入包含檔 fstream 與 string.h
¾ 利用 open 開啟檔案
¾ 利用 close 關閉檔案
Write
fstream oFile("filenme.txt",ios::out);
oFile << "test1";
oFile.close();
¾ 開檔時便指定為 out
¾ 利用 "<<" 的方式寫入資料
Read string str1;
fstream iFile;
iFile.open("test1.txt");
getline(iFile,str1);
…
iFile.close();
¾ 利用 getline()取得資料
¾ 資料會存於 str1 中
練習:
寫一個程式可以讀出 test1.txt 內所有的內容 Ans:
string str1, all;
fstream iFile;
iFile.open("test1.txt");
while(iFile){
getline(iFile,str1);
all = all + str1 +"\n";
}
cout << all << endl;
iFile.close();
範例: (Write / Read)
下面程式執行後結果會印出甚麼?
fstream oFile("test1.txt",ios::out);
oFile << "mary 1" << endl;
oFile << "john 2" << endl;
oFile.close();
fstream iFile;
string str1;
iFile.open("test1.txt");
while(iFile){
getline(iFile,str1);
if (str1[5]=='2')
cout << str1 << endl;
}
iFile.close();
Ans:
john 2
範例: (Write / Read)
下面程式執行後結果會印出甚麼?
fstream oFile("test1.txt",ios::out);
oFile << "mater 1" << endl;
oFile << "mary 2" << endl;
oFile.close();
fstream iFile;
string str1;
iFile.open("test1.txt");
while(iFile){
getline(iFile,str1);
if ((str1[0]=='m') && (str1[1]=='a')) cout << str1 << endl;
}
iFile.close();
Ans:
mater 1 mary 2
6.2
MD-DOS 畫面處理
(1). ANSI 控制游標(cursor)
基本觀念:
¾ 為主要是為 C/C++程式提供 windows 相關的功能 (Windows API) 包含變數與相關函式
¾ 某些功能需要連結特殊的 Lib 如*.lib (或*.a)
¾ 部分的功能還需要連結相關的 header files 如 excpt.h – Exception handling
基本使用方式
#include <windows.h>
HANDLE wHnd; //Handle to write to the console HANDLE rHnd; //Handle to read from the console void get_window_handles() {
wHnd=GetStdHandle(STD_OUTPUT_HANDLE);
rHnd=GetStdHandle(STD_INPUT_HANDLE);
}
void gotoxy(short x, short y){
COORD c;
c.X=x; c.Y=y;
SetConsoleCursorPosition(wHnd,c);
}
¾ 包含 windows.h
¾ 宣告 windows 畫面的讀寫變數
HANDLE wHnd; //Handle to write to the console HANDLE rHnd; //Handle to read from the console
¾ 取得 windows 相關資訊 get_window_handles();
¾ 使用方式
(1) get_window_handles();
(2) gotoxy(columns,lines);
範例: (Append)
下面程式利用 windows.h 的方式來控制游標,所提供的函式為 gotoxy(short,short),
則若要程式開執行時游標置於 name:的後面,則 varX 與 varY 要設為多少?
#include <windows.h>
…
int main(){
int varX = ?;
int varY = ?;
char name[10];
get_window_handles();
printf(“********* TEST *********\n”);
printf(“name:\n”);
printf(“addr:\n”);
gotoxy(varX,varY);
scanf(“%s”,name);
gotoxy(varX,varY+1);
scanf(“%s”,addr);
system(“pause”);
}
Ans: varX = 5, varY = 1
範例:
下面程式利用 windows.h 的方式來控制游標,所提供的函式為 gotoxy(short,short),
則若要程式開執行時游標置於 name:的後面,則 varX 與 varY 要設為多少?
#include <windows.h>
…
int main(){
int varX = ?;
int varY = ?;
char name[10];
get_window_handles();
printf(“********* TEST *********\n”);
printf(“************************\n”);
printf(“Title Name:\n”);
printf(“Address:\n”);
gotoxy(varX,varY);
scanf(“%s”,name);
gotoxy(varX,varY+1);
scanf(“%s”,addr);
system(“pause”);
}
Ans: varX = 11, varY = 2
6.3
綜合練習
(1). 以 FILE 方式來處理檔案 (05-01)
本練習要撰寫一個處理檔案的功能
¾ 處理檔案包含
(1) 提供輸入的資料的儲存 (2) 提供資料表列
(3) 依編號查詢 (4) 依名稱查詢
(2). ANSI 控制游標的方式來設計輸入畫面 (05-02)
本練習要撰寫一個資料輸入的畫面(以 FILE 方式儲存)
¾ 客戶的資料輸入 (1) 客戶編號 (2) 客戶姓名 (3) 電話 (4) 會員等級 (5) 地址
¾ 需要提供一個選單式的介面,並具有:
(1) 新增客戶 (2) 列出客戶資料 (3) 修改客戶資料 (4) 刪除客戶資料 (5) 離開程式
按下指定的鍵來執行相關功能