C语言_注释转换

实现功能:对指定路径下的一个文件夹里边的所有.c文件进行注释转换,然后在把这些文件存到指定路径的文件夹下(注释转换:把C语言注释 /* …… */ 转换为 / /……)

代码如下:

头文件代码 CommentConvert.h
#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

#define _CRT_SECURE_NO_WARNINGS 1
#define FILE_NAME 0

#include <stdio.h>  
#include <io.h>  
#include <stdlib.h>
#include <string.h>  

enum State 
{
	NUL_STATE,//无状态
	C_STATE,//C语言状态
	CPP_STATE,//c++状态
	END_STATE//结束状态
};

void Docommentconvert (FILE *pfIn, FILE *pfOut);

void DoNulState (FILE *pfIn, FILE *pfOut, enum State *p);

void DoCState (FILE *pfIn, FILE *pfOut, enum State *p);

void DoCppState (FILE *pfIn, FILE *pfOut, enum State *p);

#endif//__COMMENT_CONVERT_H__
主要函数函数代码 main.c
#include "CommentConvert.h"

void DoCppState (FILE *pfIn, FILE *pfOut, enum State *p)  //  
{
	int first = fgetc(pfIn);
	switch (first)
	{
	case '/n':
		fputc(first, pfOut);
		*p = NUL_STATE;
		break;
	case EOF:
		*p = END_STATE;
		break;
	default :
		fputc(first, pfOut);
		*p = CPP_STATE;
		break;
	}

}

void DoCState (FILE *pfIn, FILE *pfOut, enum State *p)
{
	int first = fgetc(pfIn);
	int second = 0;
	int third = 0;
	int fourth = 0;
	switch (first)
	{
	case '*':
		{
			second = fgetc(pfIn);
			switch (second)
			{
			case '/':
				{
					third = fgetc(pfIn);
					switch (third)
					{
					case EOF:
						*p = END_STATE;
						break;
					case '\n'://注释结束之后换行
						fputc('\n', pfOut);
						*p = NUL_STATE;
						break;
					case '/':
						{
							fourth = fgetc(pfIn);
							switch (fourth)
							{
							case '*':
								fputc('\n', pfOut);
								fputc('/', pfOut);
								fputc('/', pfOut);
								*p = C_STATE;
								break;
							default :
								fputc('\n', pfOut);
								fputc(third, pfOut);
								fputc(fourth, pfOut);
								*p = NUL_STATE;
								break;
							}
					
							break;
						}
					default ://注释结束之后又遇到正常代码
						fputc('\n', pfOut);//由于之前这一行打印了//所以遇到代码要先换行, 在打印正常代码
						fputc(third, pfOut);
						*p = NUL_STATE;
						break;
					}
				}
				break;
			case '*':
				ungetc (second, pfIn);
				fputc ('*', pfOut);//    遇到类似/******/情况时,读到第三个*开始,后面读到的*都要还回去, 否则可能会错过"*/“的注释结束
				*p = C_STATE;
				break;
			default :
				fputc (first, pfOut);
				fputc (second, pfOut);
				break;
			}
		}
		break;
	case '\n'://C语言注释/*…*/内部有换行时
		{
			second = fgetc(pfIn);
			switch (second)
			{
			case '*':
				{
					third = fgetc(pfIn);
					switch (third)
					{
					case '/'://换行之后注释就结束了 ,就不用再打印//
						fputc('\n',pfOut);
						*p = NUL_STATE;
						break;
					default:
						*p = C_STATE;
						break;
					}
				}
				break;
			default ://	换行之后注释没有结束, 遇到的还是注释内容,在内容前加上//,在直接打印内容
				fputc ('\n', pfOut);
				fputc ('/', pfOut);
				fputc ('/', pfOut);
				fputc (second, pfOut);
				*p = C_STATE;
				break;
			}
		}
		break;
	default:
		fputc (first, pfOut);
		*p = C_STATE;
		break;
	}

}

void DoNulState (FILE *pfIn, FILE *pfOut, enum State *p)
{
	int first = fgetc(pfIn);
	int second = 0;
	switch (first)
	{
	case '/':
		{
			second = fgetc(pfIn);
			switch (second)
			{
			case '/':
				fputc('/', pfOut);
				fputc('/', pfOut);
				*p = CPP_STATE;
				break;
			case '*':
				fputc('/', pfOut);
				fputc('/', pfOut);
				*p = C_STATE;
				break;
			case EOF:
				*p = END_STATE;
				break;
			default:
				fputc(first, pfOut);
				fputc(second, pfOut);
				break;
			}
		}
		break;
	case EOF:
		*p = END_STATE;
		break;
	default:
		fputc(first, pfOut);
		*p = NUL_STATE;
		break;
	}
}

void Docommentconvert (FILE *pfIn, FILE *pfOut)
{
	enum State ps = NUL_STATE;
	while (ps != END_STATE)
	{
		switch (ps)
		{
		case NUL_STATE:
			DoNulState (pfIn, pfOut, &ps);
			break;

		case C_STATE:
			DoCState (pfIn, pfOut, &ps);
			break;

		case CPP_STATE:
			DoCppState (pfIn, pfOut, &ps);
			break;

		case END_STATE:
			break;

		default :
			break;
		}
	}
	printf ("转换成功!!!\n");
}
测试代码:test.c
#include"CommentConvert.h"

#if 0 //这个函数的功能只能对单个文件进行操作(只能转换input.c文件里边的注释写入output.c文件)
void test ()
{
	FILE *pfIn = NULL;
	FILE *pfOut = NULL;

	pfIn = fopen ("input.c", "r");
	if (pfIn == NULL)
	{
		perror("open files for read:");
		exit (1);
	}
	pfOut = fopen ("output.c", "w");
	if (pfOut == NULL)
	{
		fclose(pfIn);
		perror ("open files for write:");
		exit (1);
	}

	Docommentconvert (pfIn, pfOut);

	fclose(pfIn);
	pfIn = NULL;
	fclose(pfOut);
	pfOut = NULL;
}
#endif

void test1 ()//可以对一个文件夹下是所有 .c 文件进行注释转换,然后将多个.c 文件再写入另一个文件夹下去
{
	FILE *fpin = NULL;
	FILE *fpout = NULL;

	char ch = 0;
	struct _finddata_t cfile;

	char path[50] = "D:\\in\\";
	char path1[50] = "D:\\in\\";

	char path_out[50] =  "D:\\out\\";

	long a;

	if ((a = _findfirst (strcat (path, "*.c"), &cfile)) == -1L)
	{
		printf ("路径错误!!!\n");
		system ("pause");
		return ;
	}

	do  
    {
		strcpy (path1, "D:\\in\\");
		strcpy (path_out, "D:\\out\\");

        if( (cfile.attrib == _A_ARCH||_A_HIDDEN||_A_RDONLY||_A_SUBDIR||_A_SYSTEM) && ~strcmp(cfile.name,".")&& ~strcmp(cfile.name,".."))  
		{
			printf("%s :",cfile.name); 
			strcat (path1,cfile.name);
			strcat (path_out,cfile.name);

			//printf ("path = %s\n", path);
			//printf ("path1 = %s\n", path1);
			//printf ("path_out = %s\n", path_out);

			fpin = fopen (path1, "r");//打开 fpin 文件
			if (fpin == NULL)
			{
				printf ("打开文件错误!!!\n");
				system ("pause");
				return ;
			}

			fpout = fopen (path_out, "w");//打开fpout 文件
			if (fpout == NULL)
			{
				printf ("打开文件错误!!!\n");
				fclose (fpin);
				system ("pause");
				return ;
			}

			Docommentconvert (fpin, fpout);
			printf ("转换之后的路径为:%s\n\n", path_out);

			fclose (fpout);
			fclose (fpin);
				
		}
    }while(_findnext(a,&cfile) == 0); /* 成功找到时返回0*/  

	printf("\n");  
    _findclose(a);  

}
int main()
{
	test1 ();


	system ("pause");
	return 0;
}

运行截图:


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值