#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool char
#define MAX 1000000
char buf[MAX + 2];
char fin[9000],fout[9000];
#if 0
FILE* readinput(const char* prompt, char* buff, int maxlen){
    int len;
    bool inok=0;
    FILE* openfile;
    do{
        inok=1;
        fprintf(stderr,"%s",prompt);
        fgets(buff, maxlen, stdin);
        len=strlen(fin);
        if(len == 1){
            fprintf(stderr,"你沒輸入!");
            inok=0;
        }else if(buff[len - 1] != '\n'){
            while(getchar()!='\n'); /* flush 掉 stdin buffer */
        }else{
            buff[len - 1] = '\0';
        }
        if(openfile=fopen())
    }while(!inok);
}

懶的寫這段了,註解掉!

#endif
int main(int argc, char* argv[])
{
    if(argc >= 2){
        fprintf(stderr,"這個程式不需要額外參數\n");
        return 2;
    }
    bool toolong=0,newsec=1,seccom=1;
    bool inok=0;
    int len;
    int c;
    FILE *in,*out;
    while(1){
        do{
            fprintf(stderr,"輸入檔名稱:");
            fgets(fin, 8999, stdin);
            len=strlen(fin);
            if(fin[len-1]=='\n'){
                fin[len-1]='\0';
            }else{
                while(getchar()!='\n'); /* flush stdin buffer */
            }
            if((in=fopen(fin,"r"))==NULL){
                perror(fin);
                inok=0;
            }else{
                inok=1;
            }
        }while(!inok);
        do{
            fprintf(stderr,"輸出檔名稱:");
            fgets(fout, 8999, stdin);
            len=strlen(fout);
            if(fout[len-1]=='\n'){
                fout[len-1]='\0';
            }else{
                while(getchar()!='\n'); /* flush stdin buffer */
            }
            if((out=fopen(fout,"w"))==NULL){
                perror(fin);
                inok=0;
            }else{
                inok=1;
            }
        }while(!inok);
        toolong=0,newsec=1,seccom=1;
        while(1){
            fgets(buf, MAX + 1, in);
            if(feof(in)){
                break;
            }
            toolong=0,seccom=0;
            len=strlen(buf);
            if(len == 0){ /* 應該不會發生,此段可刪 */
                fprintf(stderr,"發生不明錯誤,立即中止!\n");
                return 1;
            }
            if(len == 1){ /* 空白行 */
                if(newsec){ /* 判斷連續一堆空白行 */
                    seccom=1;
                    continue;
                }
                fprintf(out,");\n");
                newsec=1;
                seccom=1;
                continue;
            }
            if(buf[len - 1] != '\n'){ /* 如果字數超過 MAX */
                toolong=1;
            }else{
                buf[len - 1]='\0';
            }
            if(newsec){
                fprintf(out,"document.write(\n"
                        "\'%s",buf);
                newsec=0;
            }else{
                fprintf(out,"+\'%s",buf);
            }
            if(toolong){
                do{
                    fgets(buf, MAX + 1, in);
                    len=strlen(buf);
                    if(buf[len - 1] == '\n'){
                        buf[len - 1]='\0';
                        toolong=0;
                    }
                    fprintf(out,"%s",buf);
                }while(toolong);
            }
            fprintf(out,"\'\n");
        }
        if(!seccom){ /* 我不知結尾有沒有空白行,所以多一個判斷 */
            fprintf(out,");\n");
        }
        fclose(in);
        if(fclose(out)){
            perror(fout);
        }
        fprintf(stderr,"轉換下一個檔案?[Y] ");
        if((c=getchar()) == 'n' || c == 'N'){
            return 0;
        }else{
            if(c!='\n'){
                while(getchar()!='\n'); /* flush stdin buffer */
            }
        }
    }
    return 0;
}