程序设计2

#include
#include

int main()
{
    char c1,c2;
    FILE *fp1,*fp2;
    if(fp1=fopen("source.c","r")==NULL)
    {
        printf("Cannot open file!");
        exit(1);
    }
    if(fp2=fopen("newsource.c","w+"))
    {
        printf("Cannot build newfile");
        exit(1);
    }
    c1='\0';
    while(!feof(fp1))
    {
        c2=fgetc(fp1);
        if((c2=='{'||c2=='}')&&(c1!='\n'))
           fputc('\n',fp2);
        else if((c1=='{'||c1=='}')&&(c2!='\n'))
                fputc('\n',fp2);
        fputc(c2,fp2);
        c1=c2;
    }
    rewind(fp2);
    printf("经过处理后的源程序是\n");
    char line[300];
    int n = 1;
    while (!feof(fp2))
    {
        fgets(line,300,fp2);
        printf("%d\t%s", n, line);
        n++;
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}

你可能感兴趣的:(程序设计2)