Home > Enterprise >  How to skip blank line of text while using fgets to print text (C)
How to skip blank line of text while using fgets to print text (C)

Time:06-29

dnd.csv file

dnd.csv file

I am a beginner to C, and I'm attempting to read a file (attached) with fgets.

#include <stdio.h>

int main()
{
    FILE *f;
    f = fopen("dnd.csv", "r");
    char *var;
    char varchar[301];
    var = &varchar[0];
    int i = 0;
    int j = 0;
    
    while (1)
    {
        fgets(var, 100, f);
        if (feof(f))
        {
            break;
        }
        //printf("%s\n", var);
        i = 0;
        while (*(var   i) != ',')
        {
            if (varchar[0] != '\n')
            {
                printf("%c", *(var   i));
                i = i   1;
            }
        }
        printf(" is character %d\n", j);
        j = j   1;
    }
    fclose(f);
    return 0;
}

When I read and print it, it reads:

is character 0

Will is character 1

Mike is character 2

Dustin is character 3

Lucas is character 4

I want it to print without the first line so Will can be character 0 and Lucas 3.

I've been trying to figure out how to ignore the first line, but I haven't had any luck.

Any suggestions would be greatly appreciated.

CodePudding user response:

You can skip the first by ignoring the first read of fgets(var, 100, f); with "condition variable".

#include <stdio.h>

int main()
{
    FILE *f;
    f = fopen("dnd.csv", "r");
    char *var;
    char varchar[301];
    var = &varchar[0];
    int i = 0;
    int j = 0;
    
    int skipStep = 1;
    while (1)
    {
        fgets(var, 100, f);
        if(skipStep)
        {
            skipStep = 0;
            continue;
        }
        if (feof(f))
        {
            break;
        }
        //printf("%s\n", var);
        i = 0;
        while (*(var   i) != ',')
        {
            if (varchar[0] != '\n')
            {
                printf("%c", *(var   i));
                i = i   1;
            }
        }
        printf(" is character %d\n", j);
        j = j   1;
    }
    fclose(f);
    return 0;
}

Then your output will be like this.

enter image description here

however you code is not very readable nor clean, as you have separate whiles to print the name and other to determine the commas, i would suggest something more simple like this.

#include <stdio.h>

int main()
{
    int ch ,cntPeople = 0;
    int skipFlag = 1, skipFirstLine = 1;
    FILE *f;
    f = fopen("dnd.csv", "r");
    while((ch = fgetc(f)) != EOF)
    {
         if('\n' == ch) /* Skip the first line and help with skip other text*/
         {
            skipFlag = 0;
            if(skipFirstLine)
            {
                skipFirstLine = 0;
                continue;
            }
         }
      if(skipFlag)
      {
          continue;
     }
     if(',' == ch) /* slip text after the names*/
     {
        skipFlag = 1;
        printf(" is character %d", cntPeople  );
        continue;
     }
     putchar(ch);
    }
    fclose(f);
    return 0;
}

with the output.

enter image description here

Hope this is Helps.

CodePudding user response:

If the character is empty, i will be 1 when the while loop ends. So check for that before printing "is character".

if (i != 0) {
    printf(" is character %d\n", j);
}

A much simpler solution is to just skip over the header line by calling fgets() once before the loop.

#include <stdio.h>

int main()
{
    FILE *f;
    f = fopen("dnd.csv", "r");
    char varchar[301];
    int i = 0;
    int j = 0;

    fget(var, 100, f); // Skip header line
    
    while (fgets(varchar, sizeof varchar, f))
    {
        //printf("%s\n", var);
        i = 0;
        while (varchar[i] && varchar[i] != ',' && varchar[i] != '\n')
        {
            printf("%c", varchar[i]);
            i = i   1;
        }
        printf(" is character %d\n", j);
        j = j   1;
    }
    fclose(f);
    return 0;
}

I've also simplified the loop condition by testing the return value of fgets(). It will return NULL when it reaches EOF.

And in the loop over the characters in the line, it should stop when it reaches newline or the null terminator of the string, in case there's no , in the line.

  • Related