注释转换

来源:互联网 发布:童星大道网络选举 编辑:程序博客网 时间:2024/05/22 07:47

将C语言注释转换成C++注释

例如:

// 1.一般情况

/* int i = 0; */


// 2.换行问题

/* int i = 0; */int j = 0;

/* int i = 0; */

int j = 0;


// 3.匹配问题

/*int i = 0;/*xxxxx*/


// 4.多行注释问题

/*

int i=0; 

int j = 0;

int k = 0;

*/int k = 0;


// 5.连续注释问题

/**//**/


// 6.连续的**/问题

/***/


// 7.C++注释问题

// /*xxxxxxxxxxxx*/

对上面几种情况的转换问题进行讨论。

代码如下:

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<errno.h>typedef enum State{C_BEGIN,C_END,}State;typedef enum Convert{FILE_ERROR,SUCCESS,}Convert;void Transform(FILE* fIn, FILE* fOut){assert(fIn);assert(fOut);State flag = C_END;char first, second;do{first = fgetc(fIn);switch (first){case '/':second = fgetc(fIn);if ('*' == second){// 匹配问题if (C_END == flag){flag = C_BEGIN;fputc('/', fOut);fputc('/', fOut);}else{fputc('/', fOut);fputc('*', fOut);}}else if ('/'==second){char next;fputc('/', fOut);fputc('/', fOut);do{next = fgetc(fIn);fputc(next, fOut);} while ('\n' != next && EOF != next);}else{fputc(first, fOut);fputc(second, fOut);}break;case'*':second = fgetc(fIn);if ('/' == second){// 换行问题fputc('\n', fOut);char next = fgetc(fIn);if ('/' == next){//连续注释问题fseek(fIn, -1, SEEK_CUR);//fseek 用于二进制方式打开的文件,移动文件读写指针位置.}else if (next != '\n' && next != EOF){fputc(next, fOut);}else if ('/' == next){fseek(fIn, -1, SEEK_CUR);}else;flag = C_END;}else if ('*' == second){//连续的**/问题fputc(first, fOut);fseek(fIn, -1, SEEK_CUR);}else {fputc(first, fOut);fputc(second, fOut);}break;case '\n'://多行注释问题fputc('\n', fOut);if (C_BEGIN == flag){fputc('/', fOut);fputc('/', fOut);}break;default:fputc(first, fOut);break;}} while (first != EOF);}Convert AnnotationConversion(const char* inputFile, const char* outputFile){FILE* fIn;FILE* fOut;fIn = fopen(inputFile, "r");if (fIn == NULL){printf("打开文件%s失败!errno:%d", inputFile, errno);return FILE_ERROR;}fOut = fopen(outputFile, "w");if (fOut == NULL){fclose(fIn);//注意此处需要关闭fIn,防止对fIn打开失败对fOut的判断printf("打开文件%s失败!errno:%d", outputFile, errno);return FILE_ERROR;}Transform(fIn, fOut);//需要关闭fIn,fOutfclose(fIn);fclose(fOut);return SUCCESS;}int main(){Convert ret = AnnotationConversion("input.c", "output.c");if (ret == FILE_ERROR)printf("打开文件错误!\n");else if (ret == SUCCESS)printf("转换成功!\n");system("pause");return 0;}

运行结果如下:

// 1.一般情况

// int i = 0; 


// 2.换行问题

// int i = 0; 

int j = 0;

// int i = 0; 

int j = 0;


// 3.匹配问题

//int i = 0;/*xxxxx


// 4.多行注释问题

//

//int i=0;

//int j = 0;

//int k = 0;

//

int k = 0;


// 5.连续注释问题

//

//


// 6.连续的**/问题

//*


// 7.C++注释问题

// /*xxxxxxxxxxxx*/

小知识:

你新建了文件,后面没有扩展名,这就是文件夹选项的问题了,步骤如下:

你先打开任意一个文件夹;

再在菜单栏找到工具选项;

文件夹选项;

然后点击查看;

找到一个叫“隐藏已知文件类型的扩展名”复选框;

把那个√去掉;

再点确认OK了。

fseek函数:

  int fseek( FILE *stream, long offset, int origin );

 fseek 用于二进制方式打开的文件,移动文件读写指针位置.
  第一个参数stream为文件指针
  第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
  第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
  SEEK_SET: 文件开头
  SEEK_CUR: 当前位置
  SEEK_END: 文件结尾
  其中SEEK_SET,SEEK_CURSEEK_END和依次为012.
  简言之:
  fseek(fp,100L,0);fp指针移动到离文件开头100字节处;
  fseek(fp,100L,1);fp指针移动到离文件当前位置100字节处;
  fseek(fp,100L,2);fp指针退回到离文件结尾100字节处。

本文出自 “Scen” 博客,请务必保留此出处http://10741357.blog.51cto.com/10731357/1726175

0 0
原创粉丝点击