开发者

How I can skip a blank line in an input file when using strtok?

开发者 https://www.devze.com 2023-03-11 08:43 出处:网络
I wa开发者_如何学编程nt to pass lines of a file using strtok; the values are comma separated. However, strtok also reads blank lines which only contain spaces. Isn\'t it suppose to return a null point

I wa开发者_如何学编程nt to pass lines of a file using strtok; the values are comma separated. However, strtok also reads blank lines which only contain spaces. Isn't it suppose to return a null pointer in such a situation?

How can I ignore such a line? I tried to check NULL, but as mentioned above it doesn't work.


void function_name(void)
{

  const char delimiter[] = ",";
  char line_read[9000];
  char keep_me[9000];
  int i = 0;

  while(fgets(line_read, sizeof(line_read), filename) != NULL)
  {
      /*
       * Check if the line read in contains anything
       */
      if(line_read != NULL){
          keep_me[i] = strtok(line_read, delimiter);
          i++;
          }
  }

}

So to explain.

You're reading in your file using a while loop which reads the entire file line by line (fgets) into the array line_read.

Every time it reads in a line it will check to see if it contains anything (the NULL check).

If it does contain something it was parse it using strtok and read it into keep_me otherwise it will stay in the line_read array which you obviously don't use in your program.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号