Home > other >  How to skip a particular line from a text file if that line has white space or a particular characte
How to skip a particular line from a text file if that line has white space or a particular characte

Time:03-01

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:

  1. skip lines containing '#' in it which are for comments
  2. 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

CodePudding user response:

use strchr

while (fgets(buffer, sizeof(buffer), fp)) 

{                                                                                                                                   
    if(strchr(buffer, '#') != NULL)
    {
        continue;
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
     printf("%s", buffer); 
 }  
}

it returns NULL if char not in string

CodePudding user response:

Assuming that a line that contains a '#' is retained if it is not the first non-white-space letter:

To skip if line begins with a white-space

    if(isspace(buffer[0])) continue;

Otherwise to skip if line begins with a #

    if(buffer[0] == '#') continue;

Together

while (fgets(buffer, sizeof(buffer), fp)) { 
  unsigned char first = buffer[0];
  if(isspace(first) || (first == '#')) continue;
  ...
}

Otherwise search for a '#' as suggested by pm100.

  • Related