物件導向程式設計考古題_文檔複製(處理)_四種程式語言實作方法_c有辦法實作物件導向嗎?

一個非選題題組(共分三小題)

請寫一文字檔案 copy 函式(method or function),textFileCopy(),滿足以下需求:
可以使用任何熟悉的程式語言)(25 分)

(一)輸入及輸出檔名以 String 的參數(parameter)傳入

(二)以產生StreamReader/Writer 的物件開檔,若開檔不成功,
throw FileNotFoundException
給呼叫者(caller)。

(三)使用 try-catch 來處理讀寫(read/write)的動作,若讀寫不成功,catch IOException
關閉輸入及輸出檔案,並 throws IOException 給呼叫者。



這裡我就用

C語言

以及

C++ 、  C# 、 Java
這三門OO程式語言


做題目練習


這裡假定我們在此目錄下有一份  txt檔案
內容存有
I have an apple for lunch. 3/14

(有空白、數字、斜線及英文字母)

路徑為 : C:\TEMP
檔案名 : word.txt

=======================================================================
C 版本.
IDE  使用  Codeblocks

在複製一個檔案之前  我們要先知道如何開啟(讀取)一個檔案


文件的複製  其實乍看之下是一個對一個檔案點選之後
右鍵 ---> 複製  之後再貼上

這是牽扯到兩個操作細節流程的
Step1.先讀取
Step2.再寫入


Step1. 檔案讀取


執行後顯示


所以只會打印出  第一個字元  'I'  之後就結束了
電腦判定已經到文件的結尾

第一版.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
///C語言第一版
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE * in;///給定一個文件指標
    int ch;
    if( (in = fopen("C:\\TEMP\\word.txt" , "r"))!=NULL )
    {
        while( (ch=getc(in))!=EOF) ///從in檔案指標所指向的file位址中讀取字元
        {
            putc(ch , stdout);///打印於終端機中
            fclose(in);
        }
    }
    else
    {
        printf("There is no file here!!\n\n");
    }
    return 0;
}

原因在於我的fclose()放錯地方




進行C語言第二版的改寫_修正可成功讀取並顯示單一文件全部之內容


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
///C語言第二版
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE * in;///給定一個文件指標
    char path[100] = "C:\\TEMP\\word.txt" ;///檔案所在地為何處???用字元陣列存放
    in = fopen(path , "r");///將條件結果單一提出作指定

    if( in==NULL ) ///改採先判定是否存在的先後邏輯
    {
        printf("There is no file here!!\n\n");
    }
    else
    {
        printf("There is a file here!!\n\n");
        ///while( (ch=getc(in))!=EOF) ///從in檔案指標所指向的file位址中讀取字元
        while(!feof(in)) ///改用feof()函數 當其返回非0則代表到文件結尾!!!
        {
            char ch = fgetc(in);///從file提取所有種類字元(包含space!!!)
            putchar(ch);
            ///putc(ch , stdout);///打印於終端機中
            ///
        }
        fclose(in);
    }
    return 0;
}


這裡我將  putc  改為   putchar
兩者的功能基本上差異不大

參考 link :
http://www.tutorialspoint.com/c_standard_library/c_function_putchar.htm

(ch=getc(in))!=EOF

EOF  ---->  -1  文件結尾 End Of File
改為更簡潔有力的
!feof(in)

fclose(in);  一樣注意擺放位置!!!

Step2. 檔案寫入

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
///C語言第三版_添加文件寫入(複製)
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE * in;///給定一個文件指標
    FILE * out;///給定一個輸出文件指標
    
    char path[100] = "C:\\TEMP\\word.txt" ;///檔案所在地為何處???用字元陣列存放
    char pathOut[100] = "C:\\TEMP\\word_Copy.txt" ;///複製出來的檔案所在地為何處
    
    
    in = fopen(path , "r");///將條件結果單一提出作指定,藉由讀取方式開啟文件
    out = fopen(pathOut , "w");///藉由寫入方式讀一文件
    

    if( in==NULL ) ///改採先判定是否存在的先後邏輯
    {
        printf("There is no file here to read!!\n\n");
        return;
    }
    if(out ==NULL)
    {
        printf("There is no file here to write!!\n\n");
        return;
    }
    while(!feof(in))///當其返回非0則代表到文件結尾!!!
    {
        char ch = fgetc(in);
        putchar(ch);
        fputc(ch , out);///向文件寫入一個字元  ----------->  複製的功能!!!!
    }
    fclose(in);///關閉文件讀取串流
    fclose(out);///關閉文件寫入(複製)串流
    return 0;
}




我們剛剛已經將讀取及寫入兩項功能單元都進行實作測試完畢
緊接著就是照著題意去做修改

Step3. 改為題目所要的


寫一個 函式 名字取名為  textFileCopy()
功能為 文字檔案 copy

以 String 的參數傳入

這裡有一個尷尬的點
C語言並沒有  string 資料型態 此外C語言並非物件導向語言
因此有些功能和題目要求會有所限制!!!

一個心法 : 在C語言之中,字串字元陣列

將剛剛一大串程式碼改寫成 function



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
///C語言第四版_函數封裝調用改寫
#include <stdio.h>
#include <stdlib.h>

void textFileCopy( char* stringIn , char* stringOut)
{
    FILE * in;///給定一個文件指標
    FILE * out;///給定一個輸出文件指標

    in = fopen(stringIn , "r");///將條件結果單一提出作指定,藉由讀取方式開啟文件
    out = fopen(stringOut , "w");///藉由寫入方式讀一文件

    if(in==NULL)
    {
        printf("There is no file here to read!!\n\n");
        return;
    }
    if(out ==NULL)
    {
        printf("There is no file here to write!!\n\n");
        return;
    }
    while(!feof(in))///當其返回非0則代表到文件結尾!!!
    {
        char ch = fgetc(in);
        putchar(ch);
        fputc(ch , out);///向文件寫入一個字元  ----------->  複製的功能!!!!
    }
    fclose(in);///關閉文件讀取串流
    fclose(out);///關閉文件寫入(複製)串流
}

int main()
{
    ///char path[100] = "C:\\TEMP\\word.txt" ;///檔案所在地為何處???用字元陣列存放
    ///char pathOut[100] = "C:\\TEMP\\word_Copy.txt" ;///複製出來的檔案所在地為何處
    char str[100];
    printf("請輸入慾拷貝之檔案所在位置:\n\n");
    scanf("%s" ,str);
    puts(str); ///印出來做確認
    char strOut[100];
    printf("請輸入複製好後要貼上的所在位置:\n\n");
    scanf("%s" ,strOut);
    puts(strOut); ///印出來做確認


    ///函數的打包調用
    textFileCopy(str , strOut);
    return 0;
}

執行效果  確實可以copy出一個新檔案



以產生StreamReader/Writer 的物件開檔



C語言
具有函數指標(function pointer) 與結構(struct)
因此即便它並非OO語言仍可以藉此去模擬OO當中的物件實作機制

參考link:

Alternative idioms for inheritance in C

http://www.embedded.com/electronics-blogs/programming-pointers/4411013/Alternative-idioms-for-inheritance-in-C

c有辦法寫成物件導向嗎? 
http://www.programmer-club.com.tw/ShowSameTitleN/c/36555.html

以 C 語言撰寫物件導向程式
http://ccckmit.wikidot.com/cp:cobject

這裡我就不再繼續深入探討了
會偏題


=======================================================================
C++ 版本.

IDE  使用  Codeblocks

寫一個 函式 名字取名為  textFileCopy()
功能為 文字檔案 copy

(第一條件.)有輸入及輸出之string參數  各自代表的是  檔案名稱(fileName) input 及 output


緊接著我們要思考 如何實踐
複製(copy)一個文字檔案
複製(copy)一個 txt file (xxx.txt)

在複製一個檔案之前  我們要先知道如何開啟檔案

C++中提供了  四個檔案的 FileStream 的類別
專門用來處理  檔案的存取喔!!!
定義在此標頭檔之中
#include<fstream>
記得引用!!!

filebuf     ---> 建立暫存緩衝區
ifstream   ---> 處理檔案的input(唯讀)
ofstream  ---> 處理檔案的output(唯寫)
fstream    ---> 處理檔案的input/output(可讀寫)

參數參考link: http://www.cplusplus.com/reference/fstream/fstream/



首先打開這份檔案
這裡如果對  i開頭跟o開頭的用法覺得不太熟悉
就先用 fstream 較為彈性的  類別型態實作


在  Codeblocks 打到一半的時候
我們可以看到一些關於後頭參數的資訊叮嚀


若參數內容看不懂
我們就先上官方網站查看使用範例

http://www.cplusplus.com/reference/fstream/fstream/open/



第一版.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
///C++第一版.文件讀取及寫入(Copy功能單元實作)
#include <iostream>
#include <cstdlib>
#include<fstream>  /// std::fstream
using namespace std;

const int strMax = 100;

int main(int argc, char *argv[])
{
    char str[strMax];
    ///文件之讀取
    ifstream fileInput;
    fileInput.open("C:\\TEMP\\word.txt" , ios::in);
    ///讀取若有成功就打印該行(一次一行)
    while(fileInput.good())
    {
        fileInput.getline(str , sizeof(str));
        cout<<str<<endl;
    }

    ///文件之寫入
    ofstream fileOutput;
    fileOutput.open("C:\\TEMP\\wordCopy_with_Cpp.txt" , ios::out);
    ///使用 << 插入運算子 進行檔案一般寫入
    fileOutput << &fileInput; ///要記得前面要加 &  取位址

    ///二進位檔案流寫入--->會生看不懂之亂碼(但存取上速度較快!!)
    ///fileOutput.write((char*)&fileInput , sizeof(fileInput));///用write方法寫入讀取檔案中的資料,完成拷貝輸出

    fileInput.close();
    fileOutput.close();

    return 0;
}

再簡化第一版.

C++ 第一版簡化、先將基本Copy File功能達成 之後再去改!
用 << 運算符號 做檔案輸入


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include<fstream>  /// std::fstream

using namespace std;

int main(int argc, char *argv[])
{
    ifstream fin;
    fin.open("C:\\TEMP\\word.txt");

    ofstream fout;
    fout.open("C:\\TEMP\\wordCopyVer2.txt");

    char ch;
    while(!fin.eof()){
        fin.get(ch);
        fout<<ch;
    }
    fin.close();
    return 0;
}

第二版.改成題目要求

寫一文字檔案 copy 函式(method or function),textFileCopy()

(一)輸入及輸出檔名以 String 的參數(parameter)傳入

(二)以產生StreamReader/Writer 的物件開檔,若開檔不成功,
throw FileNotFoundException 給呼叫者(caller)。

(三)使用 try-catch 來處理讀寫(read/write)的動作,若讀寫不成功,catch IOException
關閉輸入及輸出檔案,並 throws IOException 給呼叫者。



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>
#include<fstream>  /// std::fstream
#include<exception> ///for try catch exception
using namespace std;

void textFileCopy(string input, string output)///輸入輸出之檔案名,以string參數傳入
{
 ///以產生Stream 或是 Reader/Writer 的物件開檔
 ifstream fin;
 ofstream fout;
 fin.open(input);
 fout.open(output);
 try
 {
  if (!fin)///若開檔不成功
   throw 0;
 }
 catch (...)
 {
  cout << "開檔案失敗";
 }
 char ch;
 while (!fin.eof()) {
  fin.get(ch);
  fout << ch;
 }
 fin.close();
}
int main(int argc, char *argv[])
{
 textFileCopy("C:\\TEMP\\word.txt", "C:\\TEMP\\wordCopy3.txt");
 return 0;
}























留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題