我有这个简单的代码块,它使用 fgets() 读取文件中的一行。现在我想在这一行中搜索是否有“{”或“}”并对已注释的注释进行忽略。对于函数 { 和 },我想添加这样的评论 //This is a good { 。我怎么能做到这一点?我应该使用正则表达式吗?我删除了 while 循环以简化逐行迭代。
我可以以某种方式使用 mystring,我认为它是一个数组吗?我可以追加修改它的字符串吗?或者我要做的是创建一个新数组,将 mystring 放入其中,然后放入注释。
例如:myfile.txt
/* Hello {} */
function
{
hello();
}
输出
/* Hello {} */ //Ignored
function
{ //This is a good {
hello();
} //This is a good }
我的简单块:
#include <stdio.h>
int main()
{
FILE * pFile;
char mystring [100];
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
if ( fgets (mystring , 100 , pFile) != NULL )
puts (mystring);
fclose (pFile);
}
return 0;
}