C - Program which reads text from file crashes after compiling -


i'm writing code obfuscator in c. debugger doesn't show errors in code, program crashes after compiling. i'm guessing has while loops , reading text files, i'm not sure how fix it? in advance.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>   // list off replaced elements typedef struct replacelist {     char *from; // string     char *to;   // string (random)     struct replacelist *next; } replacelist;  // ansi c keywords can't changed typedef struct data {     char *kword;     struct data *next; } data;   // add keyword list data* addtolist(data **head, data *d) {     d->next = *head;     *head = d;     return d; }   // delete elements 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 file , 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 == '/'){                 = fgetc(file);                 // until end of line                 if(a == '/'){                     while(!feof(file)){                         = fgetc(file);                         if(a == '\n')                             break;                     }                 }                 else if(a == '*'){                     while(!feof(file)){                         = fgetc(file);                         if(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 file char *getword(file *file){     char *buf = (char*)malloc(sizeof(char));     int n=0;     char a;     int ok = 0;     while(!feof(file)){         = 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 , return 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));     = random1();       kwords = fopen("ckeywords.txt","r");     // magic words     if(kwords != null){         // read data 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; } 

i see problem here. you're first closing file descriptors, "input" , "output" using fclose() , after then, you're using "input" , "output" again in while loop. that's little confusing.

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);                 } 

the fclose() function perform close() on file descriptor associated stream pointed stream. after call fclose(), use of stream causes undefined behaviour. http://pubs.opengroup.org/onlinepubs/007908799/xsh/fclose.html


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -