湖濱散記部落格的樹心幽徑[login][主頁]
518:20191023安裝Dev-C++來設計檔案內容讀取分析程式

REF 1、Dev-C++ 5.11 安裝教學(官方中文版)安裝參考:http://selfinquiring.hatenablog.com/entry/2016/03/18/204352

REF 2: DEV-C++自學手冊

REF3:網頁編碼Big5? UTF-8?

1、下載Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe(約49MB)並安裝之

2-1、編寫如下程式碼並存為m1.cpp

#include <stdio.h>
int main(void)
{
    printf("貓cat\n狗dog\n");
    return (0);
}

2-2、按F11編譯並執行m1.cpp,會於新視窗上輸出結果如下:

貓cat
狗dog

2-3、也可在命令提示字元執行m1.exe:

D:\>dir m1.*
:
2019/10/23  上午 11:47                84 m1.cpp
2019/10/23  上午 11:47           131,000 m1.exe
 :

 

D:\>m1
貓cat
狗dog

 

3-1、編寫如下程式碼並存為d:\m2.cpp

#include <stdio.h>
int main(void)
{
    FILE *outfile;
    outfile = fopen("m2.txt", "w");
    fprintf(outfile,"貓cat\n狗dog\n");
    fclose(outfile);
    printf("已將「貓cat\n狗dog\n」輸出為m2.txt");
    return (0);
}

3-2:按F11編譯並執行m2.cpp,會於新視窗上輸出結果如下:

已將「貓cat
狗dog
」輸出為m2.txt
--------------------------------
Process exited after 0.3991 seconds with return value 0
請按任意鍵繼續 . . .

3-3、也可在命令提示字元執行 type d:\m2.txt


D:\>dir m2.txt

2019/10/23  下午 04:02                14 m2.txt
               1 個檔案              14 位元組

:

D:\>type d:\m2.txt
貓cat
狗dog


3-4、在命令提示字元執行 hexdump m2.txt  (詳參 :20190930在windows命令提示字元下用hexdump來觀察文字檔案 )

D:\>hexdump m2.txt
000000  bf df 63 61 74 0d 0a aa af 64 6f 67 0d 0a

發覺以上中文字元採用二個位元組的BIG5內碼 「貓:BFDF、狗:AAAF」

 

4-1、在linux下執行m2.cpp所編出的執行檔來產生m2.txt,並顯示m2.txt如下:

$ g++ m2.cpp -o m2

$ ./m2
已將「貓cat
狗dog
」輸出為m2.txt

$ cat m2.txt
貓cat
狗dog

4-2、在linux下用hexdump指令觀看m2.txt

$hexdump m2.txt -C
00000000  e8 b2 93 63 61 74 0a e7  8b 97 64 6f 67 0a        |...cat....dog.|
0000000e

4-3在linux下編寫如下程式碼並存為m3.c

#include <stdio.h>
int main(void)
{
    FILE *infile;

    infile = fopen("m2.txt", "rb");

    fseek(infile,0, SEEK_SET );
    unsigned char ch;
    int i=0;

    printf("m2.txt的內容分析如下\n");
    while (!feof(infile)){
        fread(&ch, sizeof( char ), 1, infile);
        printf("(%d,%c,%02X,%3d)\n",i,ch,ch,ch);
        i=i+1;
    }
    fclose(infile);
    return (0);
}

 

4-4在windows的執行m3.c所編出的執行檔,結果如下

m2.txt的內容分析如下
(0,?BF,191)
(1,?DF,223)
(2,c,63, 99)
(3,a,61, 97)
(4,t,74,116)
,0D, 13)
(6,
,0A, 10)
(7,?AA,170)
(8,?AF,175)
(9,d,64,100)
(10,o,6F,111)
(11,g,67,103)
,0D, 13)
(13,
,0A, 10)
(14,
,0A, 10)

4-4、在linux下的用g++編譯m3.cpp

$ g++ m3.cpp -o m3

 

4-4、在linux下執行m3結果如下

$ ./m3

m2.txt的內容分析如下
(0,�,E8,232)
(1,�,B2,178)
(2,�,93,147)
(3,c,63, 99)
(4,a,61, 97)
(5,t,74,116)
(6,
,0A, 10)
(7,�,E7,231)
(8,�,8B,139)
(9,�,97,151)
(10,d,64,100)
(11,o,6F,111)
(12,g,67,103)
(13,
,0A, 10)
(14,
,0A, 10)

4-5在全字庫查「貓」與「狗」的各式中文內碼:

註:utf8編碼:中文字元採 3 BYTES編碼、英文字元採1 BYTES編碼。

(1)貓:\u8c93  https://www.cns11643.gov.tw/wordView.jsp?ID=94793

https://www.charbase.com/8C93

(2)狗:\u72d7 https://www.cns11643.gov.tw/wordView.jsp?ID=85616

https://www.charbase.com/72D7

 

5-1 編寫如下程式碼並存為d:\m4.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
    FILE *infile;

    if(argc!=3) {
        printf("指令語法:執行檔主檔名 輸入資料檔名 查看的位元組數\n 例如: m4 m2.txt 200\n");
        return(-1);    
    }
   
    printf("argc=%d\n",argc);
   
    printf("執行檔名 argv[0]=%s\n",argv[0]);
    printf("輸入資料檔名:argv[1]=%s\n",argv[1]);
    printf("查看的位元組數 argv[2]=%s\n",argv[2]);
   
    infile = fopen(argv[1], "rb");

    fseek(infile,0, SEEK_SET );
    unsigned char ch;
    int i=0;

    printf("%s的內容分析如下\n",argv[1]);
    while (!feof(infile) && i<atoi(argv[2]) ){
        fread(&ch, sizeof( char ), 1, infile);
        printf("(%d,%c,%02X,%3d)\n",i,ch,ch,ch);
        i=i+1;
    }
    fclose(infile);
    return (0);
}

 

5-2執行結果如下:

D:\c>m4.exe
指令語法:執行檔主檔名 輸入資料檔名 查看的位元組數
 例如: m4 m2.txt 200

D:\c>m4.exe m2.txt 8
argc=3
執行檔名 argv[0]=m4.exe
輸入資料檔名:argv[1]=m2.txt
查看的位元組數 argv[2]=8
m2.txt的內容分析如下
(0,?BF,191)
(1,?DF,223)
(2,c,63, 99)
(3,a,61, 97)
(4,t,74,116)
,0D, 13)
(6,
,0A, 10)
(7,?AA,170)

 


select id,article_id,topic,text from lt_articles_text where article_id =518; ok. update lt_articles set num_reads=num_reads +1 where id=518; ok.