總網頁瀏覽量

2011年12月12日 星期一


現在網路上很多都有提供電影字幕或是連續劇字幕的資源,也有不少學習英文的人喜歡邊看影片邊練習英聽或是會話,所以這個程式運用在練習中英閱讀及會話方面,透過隨手可得的字幕檔後,將其合併來練習。當中可練習不少開關檔案的語法,及思考排版的設計,在程式中用了4stream來協助讀檔與寫檔。

執行內容及結果
英文字幕檔內容
1
00:03:27,600 --> 00:03:28,400
Little girl?

2
00:03:30,900 --> 00:03:31,700
I'm a policeman.

3
00:03:32,700 --> 00:03:33,500
Little girl.

4
00:03:38,600 --> 00:03:39,700
Don't be afraid.

中文字幕檔內容
1
00:03:27,600 --> 00:03:28,400
小姑娘?

2
00:03:30,900 --> 00:03:31,700
我是警察

3
00:03:32,700 --> 00:03:33,500
小姑娘

4
00:03:38,600 --> 00:03:39,700
別害怕

合併中英字幕結果
Little girl?
小姑娘?

I'm a policeman.
我是警察

Little girl.
小姑娘

Don't be afraid.
別害怕

Little girl.
小姑娘

//中英字幕合併By Cheng yu-zheng
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


FILE *stream_eng, *check_eng, *stream_cht, *check_cht, *write_data;
char checkString1[100], writeString1[100], checkString2[100], writeString2[100];
int counter_eng = 2, counter_cht = 2;
int flag_eng = 0, flag_cht = 0;

void write_eng(char checkString1[], FILE *check_eng, FILE *stream_eng, FILE *write_data, FILE *check_cht, FILE *stream_cht);
int call_print(FILE *stream_eng,  FILE *write_data);
void write_cht(char checkString2[], FILE *check_cht);
int call_print_cht(FILE *stream_cht,  FILE *write_data);

int main(void)
{
        stream_eng = fopen("eng1.txt", "r");
        check_eng = fopen("eng1.txt", "r");
        stream_cht = fopen("cht1.txt", "r");
        check_cht = fopen("cht1.txt", "r");
        write_data = fopen("text.txt", "w");

        if(stream_eng == NULL || stream_cht == NULL || check_eng == NULL ||  check_cht == NULL || write_data == NULL)
        {
                printf("開始檔案失敗, 請將正確檔案放入資料夾內");
        }
        else
        {
                write_eng(checkString1, check_eng, stream_eng, write_data, check_cht, stream_eng);
        }
        fclose(stream_eng);
        fclose(check_eng);
        fclose(stream_cht);
        fclose(check_cht);
        fclose(write_data);
}

void write_eng(char checkString1[], FILE *check_eng, FILE *stream_eng, FILE *write_data, FILE *check_cht, FILE *stream_cht)
{
        while(fgets(checkString1, 100, check_eng) != NULL){
                while(checkString1[3] != ':' && atoi(checkString1) == counter_eng){
                        flag_eng = call_print(stream_eng, write_data);
                        if(flag_eng == 1){
                                counter_eng++;
                                if(counter_eng >= counter_cht)
                                        write_cht(writeString2, check_cht);
                        }
                }
        }
}

int call_print(FILE *stream_eng, FILE *write_data)
{
        while(fgets(writeString1, 100, stream_eng) != NULL){
                int stop_flag = 0;
                if(writeString1[2] != ':' && (isdigit(writeString1[0]) == 0 || strlen(writeString1) >=5) && writeString1[0] != '\n'){
                        fputs(writeString1, write_data);
                }

                if(atoi(writeString1) == counter_eng){
                        return stop_flag = 1;
                }
        }
        return 0;
}

void write_cht(char checkString2[], FILE *check_cht)
{
        int gobackto_eng = 0;
        while(fgets(checkString2, 100, check_cht) != NULL &&  gobackto_eng == 0){
                while(checkString2[3] != ':' &&  gobackto_eng == 0 && (( 0 <= checkString2[0] <= 127) ? atoi(checkString2) == counter_cht : 0)){
                        flag_cht = call_print_cht(stream_cht, write_data);
                        if(flag_cht == 1){
                                counter_cht++;
                                if(counter_cht == counter_eng)
                                        gobackto_eng = 1;  
                        }
                }
        }     
}

int call_print_cht(FILE *stream_cht, FILE *write_data)
{
        char temp[] = "\n";
        while(fgets(writeString2, 100, stream_cht) != NULL){
                int stop_flag = 0;
                if(writeString2[2] != ':' && strlen(writeString2) >=5){
                        fputs(writeString2, write_data);
                }

                if(atoi(writeString2) == counter_cht){
                        fputs(temp, write_data);
                        return stop_flag = 1;
                }
        }
        return 0;
}

沒有留言:

張貼留言