I have this simple block of code who reads a line in a file with fgets(). Now I want to search in this line if there is a "{" or a "}" and put an ignore comment on the commented ones. For the functions { and } I want to add a comment like this //This is a good { . How could I manage to do that ? Should I use regex ? I removed the while loop to simplify the line by line iteration.
Can I somehow use mystring, wich I think is an array ? Can I append mystring of modify it ? Or what I have to do is to make a new array, put mystring in it and then after put the comments.
For example : myfile.txt
/* Hello {} */
function
{
hello();
}
Output
/* Hello {} */ //Ignored
function
{ //This is a good {
hello();
} //This is a good }
My simple block :
#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;
}
Here's a simple approach, how you could process your mystring.
char *ptr=strchr(mystring,'{');
if(ptr)
{
sprintf(mystring,"%s //This is a goof {",mystring);
}
else
{
ptr=strchr(mystring,'}');
if(ptr)
{
sprintf(mystring,"%s //This is a goof }",mystring);
}
}
Hope this helps.