飞道的博客

C语言 预处理源代码空行、注释、注释块

408人阅读  评论(0)
#include <stdio.h>                                                                           
#include <stdlib.h>                                                                          
                                                                                             
#define MAX_BUFF 1024                                                                        
                                                                                             
                                                                                             
int main(int args, char *argv[]){                                                            
    int buf[MAX_BUFF];                                                                       
    int ch, next_char, i=0;                                                                  
    FILE *fpr, *fpw;                                                                         
    if (args != 2){                                                                          
        perror("Params too many");                                                           
        exit(1);                                                                             
    }                                                                                        
    fpr = fopen(argv[1], "r");                                                               
    fpw = fopen("predeal.out", "w");                                                         
    if (fpr == NULL){                                                                        
        perror("Fail to read");                                                              
        exit(1);                                                                             
    }                                                                                        
    //Begin to predeal file                                                                  
    while(EOF != (ch=fgetc(fpr))){                                                           
        if (ch == '\n') continue;                                                            
        if (ch == '/'){                                                                      
            next_char = fgetc(fpr);                                                          
            if(next_char == '/'){                                                            
                //enter into line annotation
                 while ('\n' != fgetc(fpr));                                                  
                continue;                                                                    
            }else if(next_char == '*'){                                                      
                //enter into many lines annotation inspect                                   
                while (1){                                                                   
                    next_char = fgetc(fpr);                                                  
                    if(next_char == '*'){                                                    
                        if (fgetc(fpr) == '/'){                                              
                            break;                                                           
                        } else{                                                              
                        fseek(fpr, -1L, SEEK_CUR);                                           
                    }                                                                        
                    }                                                                        
                };                                                                           
                continue;                                                                    
            }else{                                                                           
                fseek(fpr, -1L, SEEK_CUR);                                                   
            }                                                                                
        }                                                                                    
        buf[i] = ch;                                                                         
        i++;                                                                                 
    }                                                                                        
                                                                                             
    //White the predeal result                                                               
    for(int j=0;j<i;j++){                                                                    
        fprintf(fpw, "%c", buf[j]);                                                          
    } 
    return 0;                                                                                
}                                                                                            

转载:https://blog.csdn.net/Sherlock_Holmes_lv/article/details/104524894
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场