0

I have a do loop that prints out a given text file line by line. It works great, but I want the program to not print an entire line if said line starts with a specific character (in this case, #).

do
{
    c = fgetc(file);
    if (c != compare[0]) //char * compare= "#"
    {
         printf("%c",c);
    }
} while (c != EOF);
fclose(file);
return 0;

This code above just prints everything and skips only the # character itself.

TRiG
  • 10,148
  • 7
  • 57
  • 107
jjyj
  • 377
  • 4
  • 15
  • Ask yourself - what marks the end of a line? Keep reading and ignoring the character until you see that marker. Alternatively, use [`fgets`](https://linux.die.net/man/3/fgets). – kaylum Oct 10 '16 at 00:50
  • 1
    `if( topOfLine && c == '#') while((c = fgetc(file)) != '\n' && c != EOF);` – BLUEPIXY Oct 10 '16 at 00:59
  • 1
    Don't use a `do … while` loop; use a `while` loop: `int c; while ((c = getc(file)) != EOF) { … }`. – Jonathan Leffler Oct 10 '16 at 02:10

0 Answers0