I'm new to string and file operations in C and I have a text file file.txt with below data.
I'm reading a text file which contains below information
1.#comment
2.Name="Audi"
3.Class="Premium"
4. Cost="High"
5. #
I wanted to store contents of this file into a buffer with below conditions:
- skip lines containing '#' in it which are for comments
- skip lines which are starting with white space.
while writing to the buffer.
I have used fgets() to read each line by line which checks for '#' presence in it
My sample code is below:
char buffer[64];
FILE* fp= fopen("file.txt", "r");
if (NULL == fp) {
printf("file can't be opened \n");
}
else
{
while (fgets(buffer, sizeof(buffer), fp))
{
if(buffer[0]=='#')
{
continue;
} printf("%s", buffer);
}
}
This code works fine if first character of line is #(1st line), but I wanted to skip line whenever I get a # in the line not only first one(5th line in text file needs to be skipped). Similarly I wanted to skip line if I have white space in line(4th line). I'm getting stuck here and unable to proceed further. Is there an efficient way to do the same? Please support
Edit: As suggested I tried with isspace & strchr and it is working.Thank you for support.Also one more query is whether isspace and strchr are misra 2012 complaint?
use
strchrit returns NULL if char not in string