C程序从同一文件中删除注释
下面是从C程序中删除注释的代码,但删除注释的代码存储在另一个程序中C程序从同一文件中删除注释,c,file-io,comments,C,File Io,Comments,下面是从C程序中删除注释的代码,但删除注释的代码存储在另一个程序中 #include<stdio.h> main(int argc , char *argv[]) { FILE *fp; char ch; fp=fopen(argv[1],"r"); fp1=fopen(argv[2],"w"); while(1) { ch=fgetc(fp); if(ch==EOF)
#include<stdio.h>
main(int argc , char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[1],"r");
fp1=fopen(argv[2],"w");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else
{
if(ch=='/')
{
ch=fgetc(fp);
if(ch=='/')
{
while(1)
{
ch=fgetc(fp);
if(ch=='\n')
goto label;
}
}
if(ch=='*')
{
while(1)
{
ch=fgetc(fp);
if(ch=='*')
{
ch=fgetc(fp);
if(ch=='/')
{
while(1)
{
ch=fgetc(fp);
goto label;
}
}
else
printf("*");
}
}
}
else
printf("/");
}
}
label:
fputc(ch,fp1);
}
fclose(fp);
fclose(fp1);
}
#包括
main(int argc,char*argv[])
{
文件*fp;
char ch;
fp=fopen(argv[1],“r”);
fp1=fopen(argv[2],“w”);
而(1)
{
ch=fgetc(fp);
如果(ch==EOF)
打破
其他的
{
如果(ch=='/')
{
ch=fgetc(fp);
如果(ch=='/')
{
而(1)
{
ch=fgetc(fp);
如果(ch='\n')
后藤标签;
}
}
如果(ch=='*')
{
而(1)
{
ch=fgetc(fp);
如果(ch=='*')
{
ch=fgetc(fp);
如果(ch=='/')
{
而(1)
{
ch=fgetc(fp);
后藤标签;
}
}
其他的
printf(“*”);
}
}
}
其他的
printf(“/”);
}
}
标签:
fputc(ch,fp1);
}
fclose(fp);
fclose(fp1);
}
现在我想制作一个程序,从同一个文件中删除注释。因此,当我们打开它时,注释不应该存在。请指导我,因为我不知道如何制作这样的程序?这不是一个简单的任务,使用
fgetc
您的代码在以下情况下失败:
char *s = "Comments starts with /*";
如果这不是练习,并且您正在使用gcc
,我建议使用fpreprocessed
标志跳过注释:
gcc -fpreprocessed -P -dD -E test.c > cleancode.c
然后使用diff
获取差异(注释):
diff -ignore-all-spaces test.c cleancode.c > comments.c
这不是一项简单的任务,使用
fgetc
您的代码会在以下情况下失败:
char *s = "Comments starts with /*";
如果这不是练习,并且您正在使用gcc
,我建议使用fpreprocessed
标志跳过注释:
gcc -fpreprocessed -P -dD -E test.c > cleancode.c
然后使用diff
获取差异(注释):
diff -ignore-all-spaces test.c cleancode.c > comments.c
试试这个。我测试了它本身,似乎工作。它使用不同的文件名,所以如果情况不好,原始文件仍然存在
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main (int argc , char *argv[]) {
FILE *fp;
FILE *fp1;
int c;
int c1;
int previous = 0;
int literal = 1;
if ( argc != 3) { // check for number of command line arguments
printf ( "invalid command syntax\n");
exit(1);
}
if ( strcmp ( argv[1], argv[2]) == 0) { // arguments are the same
printf ( "use different file names please\n");
exit(2);
}
fp=fopen(argv[1],"r");
if ( fp == NULL) {
printf ( "could not open read file\n");
exit(3);
}
fp1=fopen(argv[2],"w");
if ( fp1 == NULL) {
printf ( "could not open write file\n");
fclose(fp);
exit(4);
}
while ( ( c = fgetc ( fp)) != EOF) { // read file to end of file
if ( c == '"') {
if ( !( ( previous == '\\') || ( previous == '\''))) { // was previous an escaped or single quoted quote
literal = !literal;
}
}
if ( c == '/' && literal) {
if ( ( c1 = fgetc ( fp)) == '/') {
// single line comment
while ( ( c = fgetc ( fp)) != '\n') {
; // blank statement
// read to end of line
// c will be a newline
}
}
else {
if ( c1 == '*') {
/* block comment spanning
more than one line */
while ( c = fgetc ( fp)) { // keep reading block
if ( c == '*') { // may have found the end of block
if ( ( c = fgetc ( fp)) == '/') { // yes it is the end of the block
c = '\n'; // set c to newline
break; // get out of this loop
}
}
}
}
else { // this was not a comment
fputc ( c, fp1); // write c to the file
c = c1; // another character was read so set c to the other character
}
}
}
previous = c;
fputc ( c, fp1); // write c to the file
}
fclose(fp); // close the files
fclose(fp1);
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
文件*fp;
文件*fp1;
INTC;
int c1;
int-previous=0;
int-literal=1;
如果(argc!=3){//请检查命令行参数的数量
printf(“无效命令语法\n”);
出口(1);
}
如果(strcmp(argv[1],argv[2])==0{//参数相同
printf(“请使用不同的文件名\n”);
出口(2);
}
fp=fopen(argv[1],“r”);
如果(fp==NULL){
printf(“无法打开读取文件\n”);
出口(3);
}
fp1=fopen(argv[2],“w”);
如果(fp1==NULL){
printf(“无法打开写入文件\n”);
fclose(fp);
出口(4);
}
而((c=fgetc(fp))!=EOF){//将文件读到文件的末尾
如果(c==“”){
如果(!((previous=='\\')|(previous=='\'')){//previous是转义引号还是单引号
文字=!文字;
}
}
if(c=='/'&&literal){
如果((c1=fgetc(fp))=='/')){
//单行注释
而((c=fgetc(fp))!='\n'){
;//空白语句
//读到行尾
//c将是一条新线
}
}
否则{
如果(c1=='*'){
/*块注释生成
多行*/
而(c=fgetc(fp)){//保持读取块
如果(c=='*'){//可能已经找到了块的结尾
如果((c=fgetc(fp))='/'){//是,则该块结束
c='\n';//将c设置为换行符
break;//退出这个循环
}
}
}
}
否则{//这不是评论
fputc(c,fp1);//将c写入文件
c=c1;//读取了另一个字符,因此将c设置为另一个字符
}
}
}
先前=c;
fputc(c,fp1);//将c写入文件
}
fclose(fp);//关闭文件
fclose(fp1);
返回0;
}
试试这个。我对它本身进行了测试,似乎很有效。它使用不同的文件名,所以如果情况不好,原始文件仍然存在
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main (int argc , char *argv[]) {
FILE *fp;
FILE *fp1;
int c;
int c1;
int previous = 0;
int literal = 1;
if ( argc != 3) { // check for number of command line arguments
printf ( "invalid command syntax\n");
exit(1);
}
if ( strcmp ( argv[1], argv[2]) == 0) { // arguments are the same
printf ( "use different file names please\n");
exit(2);
}
fp=fopen(argv[1],"r");
if ( fp == NULL) {
printf ( "could not open read file\n");
exit(3);
}
fp1=fopen(argv[2],"w");
if ( fp1 == NULL) {
printf ( "could not open write file\n");
fclose(fp);
exit(4);
}
while ( ( c = fgetc ( fp)) != EOF) { // read file to end of file
if ( c == '"') {
if ( !( ( previous == '\\') || ( previous == '\''))) { // was previous an escaped or single quoted quote
literal = !literal;
}
}
if ( c == '/' && literal) {
if ( ( c1 = fgetc ( fp)) == '/') {
// single line comment
while ( ( c = fgetc ( fp)) != '\n') {
; // blank statement
// read to end of line
// c will be a newline
}
}
else {
if ( c1 == '*') {
/* block comment spanning
more than one line */
while ( c = fgetc ( fp)) { // keep reading block
if ( c == '*') { // may have found the end of block
if ( ( c = fgetc ( fp)) == '/') { // yes it is the end of the block
c = '\n'; // set c to newline
break; // get out of this loop
}
}
}
}
else { // this was not a comment
fputc ( c, fp1); // write c to the file
c = c1; // another character was read so set c to the other character
}
}
}
previous = c;
fputc ( c, fp1); // write c to the file
}
fclose(fp); // close the files
fclose(fp1);
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
文件*fp;
文件*fp1;
INTC;
int c1;
int-previous=0;
int-literal=1;
如果(argc!=3){//请检查命令行参数的数量
printf(“无效命令语法\n”);
出口(1);
}
如果(strcmp(argv[1],argv[2])==0{//参数相同
printf(“请使用不同的文件名\n”);
出口(2);
}
fp=fopen(argv[1],“r”);
如果(fp==NULL){