Hello guys can anyone please help me in a problem relating to file.
The problem is that I want to use fgets on a c file from another c file and increment a the counter every time to get the number of lines and I also want to put the line number is a seperate array where I enconter the string #define..
This was easy & I implemented it using fscanf & strcmp function but this code is not working if the file contains only newlines or spaces in the line but no characters, line numbers are not counted properly because of fscanf property.
That means I have to do a fuzzy substring search in the main line that I have saved in a string which should work for

#define
         #define
#   define
       #     define

mind that I cannot use fscanf because then it will not give line numbers properly for all those lines where I have no characters only space or simple a n, those lines are not scanned by fscanf

Here is my humble attempt..

#include <stdio.h>
#include <string.h>
int main(){

FILE* buffer = NULL;
FILE* temp = NULL;
char ch[100];
int newLine = 1;
char c; 
int defineLines[100], i =0,j;
if ( (buffer = fopen("./inputfile.c", "r+")) ){
  while( fscanf(buffer, "%s", ch) != EOF ){

     if( (c = getc(buffer)) == '\n' )
         newLine++;



     if( strcmp(ch, "#") == 0 ){
       fscanf(buffer, "%s", ch);
       if(strcmp(ch, "define") == 0){
         printf("#define is found\n");
         defineLines[i++] = newLine;
       }
     }else if(strcmp(ch, "#define") == 0){
       printf("#define is found\n");
       defineLines[i++] = newLine;
     }
   }
}
fclose(buffer);


for(j = 0; j < i; j++)
  printf("%d ", defineLines[j]);

newLine = 1;
i = 0;

if( (buffer = fopen("./inputfile.c", "r")) && (temp = fopen("temp.txt", "w")) )
  while(fgets(ch, sizeof(ch), buffer) != NULL){
    if(newLine++ != defineLines[i]) 
      fputs(ch, temp);
    else
      i++;
  }
fclose(buffer);
fclose(temp);
remove("inputfile.c");
rename("./temp.txt", "./inputfile.c");



return 0;
}

this working for the inputfile.c which contains no blank lines between #define but not working if there is a blank line
try for this inputfile.c

#include<stdio.h>
#include <string.h>
#define pi 3.14
#define PI 3.1
int main(){
  printf("Hello World");
  printf("%f", pi);
  return 0;
}

but not working for

#include<stdio.h>
#include <string.h>
#define pi 3.14

#define Pi 3.1
int main(){
  printf("Hello World");
  printf("%f", pi);
  return 0;
}

This new little library of C functions,
that eases the reading and parsing of a line of text in C
(or 'words' of text)
may make your problem mush easier to handle ...

http://developers-heaven.net/forum/index.php/topic,2582.msg3142.html#msg3142

It permits using C dynamic strings,
almost as easily,
as one can use C++ strings
with getline and cin

( readLine emulates getline )

( readWord emulates cin >> )

( but remember to free all dynamic memory when done with it. )

See the example of usage provided.

This example of usage may help:

Note: files found here ...

http://developers-heaven.net/forum/index.php/topic,2580.0.html

/* testIt1.c */

#include "readLine.h" 
#include "CvecOfInt.h"

const char* FNAME = "test1.txt";
/*
    #include <stdio.h>
    #include <string.h>

    #define pi 3.14
    #define Pi 3.14159

    int main()
    {
      printf("The value of PI is %f", pi);
      return 0;
    }
*/


int main()
{
    FILE* fp = fopen( FNAME, "r" );
    if( fp )
    {
        char* line;
        Rec rc;
        Cvec cv;
        int i = 0;
        initCvec( &cv );
        while( (line = readLine( fp )) )
        {
            ++i;
            if( strchr(line, '#' ) )
            {
                rc.val = i;
                push_backCvec( &cv, &rc );
            }
            free( line );
        }
        fclose( fp );

        for( i = 0; i < cv.size; ++ i )
            printf( "There was a '#' char on line: %d\n", cv.ary[i].val );

        clearCvec( &cv );
    }
    else printf( "There was a problem opening file %s\n", FNAME );

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.