-3

I want to count the lines of a c file ignoring the lines that have comments or empty lines , i came up with the following code but my string ( char pointer) only returns three chars instead of the whole line:

FILE* pf = NULL;
char *chaine;
int count=0;
int com=0;
pf=fopen("test.c","r+");
while(!feof(pf))
{
    if(com%2==0)
    count++;
    fgets(&chaine, sizeof(chaine),pf);
    if(&chaine=='\n' || &chaine==' ' || strstr(&chaine, "//") != NULL)
    {
        count--;
    }
    if(strstr(&chaine, "//*") != NULL || strstr(&chaine, "*//") != NULL)
    {
        com++;
    }
  printf("The line %d have the following string : %s\n",count,&chaine);
}
//printf("The number of lines est : %d", count);

Solved

Thanks for the answers especially for @Michael Walz i found that i have problems with my pointers. After i re-adjusted my if conditions now the line counter works just fine and this is the working code:

#include <stdio.h>
#include <string.h>

int main(void)
{
  FILE* pf = NULL;
  char chaine[1000];
  int count = 0;
  int com = 0;
  pf = fopen("test.c", "r");

  while (1)
  {
    if (com % 2 == 0 | strstr(chaine, ";//") != NULL)
      count++;

    if (fgets(chaine, sizeof(chaine), pf) == NULL)
      break;

    if (chaine[0] == '\n' || chaine[0] == "" || strstr(chaine, "//") != NULL)
    {
      count--;
    }

    if (strstr(chaine, "/*") != NULL)
    {
      com++;
    }
    if (strstr(chaine, "*/") != NULL)
    {
      com++;
    }

  }
  fclose(pf);
  printf("The file have %d line code", count);
}
Community
  • 1
  • 1
Amine Tagui
  • 205
  • 2
  • 13
  • 1
    `chaine` isn't initialized, and you should change `&chaine` by `chaine` everywhere – Jean-François Fabre Dec 05 '17 at 10:03
  • @Jean-FrançoisFabre I need to initialize it with what ? I used chaine instead of &chaine before but the console stops working ... – Amine Tagui Dec 05 '17 at 10:11
  • adding to above comment - you need to allocate space for the string you're reading in fgets. declare chaine with the longest possible line length. you know you just read a line, you don't need to check – Ora Dec 05 '17 at 10:11
  • 2
    Basically none of this code makes any sense and you have to go study pointers. Also, this will not compile. – Lundin Dec 05 '17 at 10:12
  • @Lundin details please ? – Amine Tagui Dec 05 '17 at 10:15
  • @AmineTagui Too many errors for me to write an answer. Basically, an answer would be the whole chapter about pointers in your beginner-level C book. – Lundin Dec 05 '17 at 10:17

1 Answers1

1

You probably want this:

#include <stdio.h>
#include <string.h>

int main(void)
{
  FILE* pf = NULL;
  char chaine[1000];
  int count = 0;
  int com = 0;
  pf = fopen("test.c", "r");

  if (pf == NULL)   // we abort if the file cannot be opened
  {
    printf("File cannot be opened\n");
    return 1;
  }

  while (1)  // see link below
  {
    if (com % 2 == 0)
      count++;

    if (fgets(chaine, sizeof(chaine), pf) == NULL)
      break;  // if fgets returns NULL we are presumably at the end of the file

    if (chaine[0] == '\n' || chaine[0] == ' ' || strstr(chaine, "//") != NULL)
    {
      count--;
    }

    if (strstr(chaine, "//*") != NULL || strstr(chaine, "*//") != NULL)
    {
      com++;
    }

    printf("The line %d have the following string : %s\n", count, chaine);
  }
}

This is untested code, there may be errors.

Also read this: Why is “while ( !feof (file) )” always wrong?.

I also strongly recommand you to read the chapter dealing with strings and the chapter dealing with pointers in your C text book.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115