C - 编译后从文件崩溃中读取文本的程序

时间:2014-02-06 03:37:35

标签: c file-io while-loop

我在C中编写代码混淆器。调试器在代码中没有显示任何错误,但程序在编译后崩溃。我猜它与while循环和从文件中读取文本有关,但我不知道如何解决它?提前谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


// list off all replaced elements
typedef struct ReplaceList
{
    char *from; // from string
    char *to;   // to string (random)
    struct ReplaceList *next;
} ReplaceList;

// ANSI C keywords that can't be changed
typedef struct Data
{
    char *kword;
    struct Data *next;
} Data;


// add keyword to list
Data* AddToList(Data **head, Data *d)
{
    d->next = *head;
    *head = d;
    return d;
}


// delete all elements from list
void FreeList(Data *head)
{
    Data *prev;
    while (head != NULL)
    {
        prev = head;
        head = head->next;
        free(prev);
    }
}


// Generating random numbers
int random1()
{
  return ((rand() % 6) + 1);
}

// return other characters from file and erase comments
char *getOtherCharacters(FILE *file){
    char *buf = (char*)malloc(sizeof(char));
    char a;
    int n=0;

    while(!feof(file)){
        a= fgetc(file);
        if(
            (!((a >= 'a') && (a<='z'))) &&
            (!((a >= 'A') && (a <= 'Z'))) &&
            (a != '_')
            )
        {
            // comment
            if(a == '/'){
                a = fgetc(file);
                // until end of line
                if(a == '/'){
                    while(!feof(file)){
                        a = fgetc(file);
                        if(a == '\n')
                            break;
                    }
                }
                else if(a == '*'){
                    while(!feof(file)){
                        a = fgetc(file);
                        if(a=='*'){
                            a = fgetc(file);
                            if(a == '/')
                                break;
                            else
                                fseek(file,ftell(file)-1,SEEK_SET);
                        }
                    }
                }
                // other chars
                else{
                    buf = (char*)realloc(buf,++n);
                    buf[n-1] = a;
                }
            }
            else{
                buf = (char*)realloc(buf,++n);
                buf[n-1] = a;
            }
        }
        else{
            fseek(file,ftell(file)-1,SEEK_SET);
            buf = (char*)realloc(buf,++n);
            buf[n-1] = '\0';
            return buf;
        }
    }
    return NULL;
}

// return word from file
char *getWord(FILE *file){
    char *buf = (char*)malloc(sizeof(char));
    int n=0;
    char a;
    int ok = 0;
    while(!feof(file)){
        a = fgetc(file);
        if(
            ((a >= 'a') && (a<='z')) || 
            ((a >= 'A') && (a <= 'Z')) ||
            ( (ok==1)&& (a>='0') && (a<='9')) ||
            (a == '_')
            )
        {
            ok = 1;
            buf = (char*)realloc(buf,++n);
            buf[n-1] = a;
        }
        else if(ok==1){
            if(!feof(file))
                fseek(file,ftell(file)-1,SEEK_SET);
            buf = (char*)realloc(buf,++n);
            buf[n-1] = '\0';
            return buf;
        }
        else
            return NULL;
    }
    return NULL;
}

// check if word exist in ReplaceList
ReplaceList *checkWord_ReplaceList(char *word,ReplaceList *Head){
    ReplaceList *replaceList;
    replaceList = Head;
    while(replaceList != NULL){
        if(strcmp(word, replaceList->from) == 0){
            return replaceList;
        }
        replaceList = replaceList->next;
    }
    return NULL;
}

// check if word exist in Data struct
Data *checkWord_Data(char *word,Data *Head){
    Data *data;
    data = Head;
    while(data != NULL){
        if(strcmp(word, data->kword) == 0){
            return data;
        }
        data = data->next;
    }
    return NULL;
}


// generate new element of ReplaceList and return it instance
ReplaceList *newReplaceListElement(ReplaceList *begin, char *from){
    int random;
    ReplaceList *tmp;
    int i;

    tmp = (ReplaceList*)malloc(sizeof(ReplaceList));
    tmp->to = (char*)malloc(sizeof(char)*11);

    for(i=0; i<10;i++)
        tmp->to[i] = rand()%26 + 'a';
    tmp->to[i] = '\0';

    tmp->from = from;
    tmp->next = begin;
    begin = tmp;
    return tmp;
}

int main(int argc, char *argv[])
{
    FILE * input;
    FILE * output;
    FILE *kwords;
    Data *dataBegin = NULL;
    ReplaceList *replaceListBegin = NULL;
    int a, i=0;
    char c;
    srand(time(NULL));
    a = random1();


    kwords = fopen("ckeywords.txt","r");
    // get all magic words
    if(kwords != NULL){
        // read data to the first list
        while (!feof(kwords))
        {
            Data *d;
            d = (Data *) malloc( sizeof(Data)); // allocate new element
            d->kword = getWord(kwords);         
            fscanf(kwords,"\n");
            AddToList(&dataBegin, d);
        }
        fclose(kwords);
    }

    input = fopen("inputcode.txt","r");
    output = fopen("outputcode.txt","w");
    // parse file
    if(input != NULL){
        while(!feof(input)){
            char *word;
            Data* data;

            word = getWord(input);
            if(word != NULL){
                data = checkWord_Data(word,dataBegin);
                if(data!=NULL){
                    fprintf(output,"%s",word);
                }
                else{
                    ReplaceList *replaceList;
                    replaceList = checkWord_ReplaceList(word,replaceListBegin);
                    if(replaceList == NULL){
                        replaceList = newReplaceListElement(replaceListBegin,word);
                    }
                    fprintf(output,"%s",replaceList->to);
                }
            }
            else{
                word = getOtherCharacters(input);
                fprintf(output,"%s",word);
            }
        }
    }

    fclose(input);
    fclose(output);

    if (strcmp(argv[i], "-i") == 0)
        while (!feof(input))
            {
                c=(char)fgetc(input);
                printf("%c", c);
            }

    if (strcmp(argv[i], "-o") == 0)
        while (!feof(output))
            {
                c=(char)fgetc(output);
                printf("%c", c);
            }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

我在这里看到一个问题。您首先使用fclose()关闭文件描述符“input”和“output”,然后,在循环中再次使用“input”和“output”。这有点令人困惑。

fclose(input);
fclose(output);

        if (strcmp(argv[i], "-i") == 0)
            while (!feof(input))
                {
                    c=(char)fgetc(input);
                    printf("%c", c);
                }

        if (strcmp(argv[i], "-o") == 0)
            while (!feof(output))
                {
                    c=(char)fgetc(output);
                    printf("%c", c);
                }
  

fclose()函数将对文件描述符执行close()   与stream指向的流相关联。之后   调用fclose(),任何使用流都会导致未定义的行为。 http://pubs.opengroup.org/onlinepubs/007908799/xsh/fclose.html