[英]remove spaces after removing comments from a c file
我已經從input.c文件中成功刪除了單行和多行注釋,但無論在input.c文件中的何處,在output.c文件中都會創建空行。 如何刪除它們?
#include<stdio.h>
int main()
{
FILE *f1,*f2;
char c;
int i=0;
f1=fopen("input.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
{
if(c=='/')
{
if((c=getc(f1))=='*' )
{
do
{
c=getc(f1);
}while(c!='*');
c=getc(f1);
if(c=='/')
c=getc(f1);
}
else
{
if(c=='/')
{
do
{
c=getc(f1);
}while(c!=10);
}
}
}
fseek(f2,1,i++);
putc(c,f2);
}
fclose(f1);
fclose(f2);
return 0;
}
c=getc(f1);
if(c=='/')
c=getc(f1);
在這里做:
if (c=='\n')
c=getc(f1); /* that will read next char from input, when last red char is '\n' */
最后一行讀取以'/'結尾的下一個字符。 僅當c不是'\\ n'時,才應將c放在輸出文件中。
同樣在這里:
do
{
c=getc(f1);
}while(c!=10);
在這里總是做:
c=getc(f1);
刪除必須在c變量中的'\\ n'以停止循環執行。
看起來像這樣才能實際工作。
#include<stdio.h>
int main()
{
FILE *f1,*f2;
char c;
f1=fopen("fahr.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
{
if(c=='/')
{
if((c=getc(f1))=='*' )
{
do
{
c=getc(f1);
}while(c!='*');
c=getc(f1);
if(c=='/')
c=getc(f1);
if(c=='\n')
c=getc(f1);
}
else
{
if(c=='/')
{
do
{
c=getc(f1);
}while(c!=10);
c=getc(f1);
}
}
}
putc(c,f2);
}
fclose(f1);
fclose(f2);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.