-2

I want to write a code which find all the lines starting with #include and write those files separately into another file.My main task is to find the names of all the include files and write them in a seprate file.

This is quite clear that we need to open and read this file using basic IO operations.After then i think using strtok function we can parse different strings between # (as include start with #), but i find this not a proper way.because in doing so we need to have array pointers which are needed to be initialized and there may ne n numbers of included files in the main file(if we take them recursively, means a include file can also have include files).

Need suggestions.

Community
  • 1
  • 1
shailendra
  • 271
  • 2
  • 6
  • 18
  • 1
    Python or grep would be good tools for this task. – Vorac Aug 30 '13 at 15:11
  • 4
    `grep '^#include' infile > outfile` is the proper way. Right tool for the job and all that. Unless you are confined to C by your professor, of course. – n. m. could be an AI Aug 30 '13 at 15:11
  • @shailendra - I believe you are being marked down here, not because your question does not have merit, but because it does not show any effort you have made in an attempt to solve the problem yourself. Nevertheless You have described it well enough to suggest you want to solve the problem using an executable written in C to parse a C file. Is this correct? – ryyker Aug 30 '13 at 16:36
  • @ryyker: Yes i want to just took out the names of all include file written in any file , it may be a verilog file or a C file or a C++ file(recursively also) and write them into any other text file. – shailendra Sep 02 '13 at 06:18
  • @ryyker - May I disagree? I see some effort in expressing a high level algorithm: read file, parse `#include`, store the result, repeat recursively. – mouviciel Sep 10 '13 at 07:07
  • 1
    @mouviciel - I was simply expressing a possible reason as to why _others_ were marking him down. As I indicated by ***Nevertheless You have described it well enough to suggest you want to solve the problem using an executable written in C to parse a C file. Is this correct?*** I found sufficient work and understanding in his statements to make an attempt at addressing them. So, actually, I believe we are in agreement. No? – ryyker Sep 12 '13 at 18:15
  • @ryyker - I read again your first comment and I understand better. So, I agre with you but not with the downvoters. – mouviciel Sep 12 '13 at 18:47

2 Answers2

1

I am still not sure exactly what you want, but it looks like you want to use an executable written in C to parse another C type file to separate the occurances of "#include" to another C type file. This is an example that should build for you in an ANSI C complier. Just pass it the complete path/filenames of the source and destination files:

#include <windows.h>
#include <ansi_c.h>  

int parse(char *in, char *out);


int main(void)
{
    //edit with desired file names        
    parse("c:\\temp\\someCfile.c"  , "C:\\temp\\someNewCfile.c");
    return 0;
}

    int parse(char *in, char *out)
    {
        FILE *fp=0, *fp2=0;
        char lineBuf[280];

        if(in)
        {
            fp = fopen(in, "r");
            if(fp)
            {
                fp2 = fopen (out, "w");
                if(fp2)
                {
                    while(fgets(lineBuf, 280,  fp))
                    {
                        if((lineBuf[0]=='#') && strstr(lineBuf, "#include"))
                        {
                            fputs(lineBuf, fp2);
                        }
                    }
                    fclose(fp2);
                }
                fclose(fp);
            }   
        }
        return 0;
    }
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • This definitely solved my problem upto certain limit,i had to certain changes so as to remove #include and "<" and ">" to get only name and in verilog, include file are between double quotes, but that help a lot. – shailendra Sep 02 '13 at 07:33
  • Glad to help. "<" etc are common in C, which is what you had tagged :) – ryyker Sep 03 '13 at 21:24
  • :So my code is done i am able to get only the names of the include files from the C file – shailendra Sep 04 '13 at 06:13
1

With some addition to Ryyker code, below code work out for me.

#include<stdio.h>
#define CHUNK 1024
int parse(char *in, char *out);


int main(void)
{  char source[100];
     printf("Enter the name of the source file : ");
     scanf("%s",source);
         printf("%s \n",source);
          parse(source  , "/home/shailendra/test.txt");  //edit with desired destination file name where file names 
    return 0;
}

    int parse(char *in, char *out)
    {
int i;
        FILE *fp=0, *fp2=0;
        char lineBuf[CHUNK];
        char *str1;
        char buf[100];
        if(in)
        {
            fp = fopen(in, "r");
            if(fp)
            {
                fp2 = fopen (out, "w+");
                if(fp2)
                {
                    while(fgets(lineBuf,CHUNK,  fp))
                    {i = strlen(lineBuf);
                     lineBuf[i-2] = 0;

                        if((lineBuf[0]== '`') && strstr(lineBuf, "`include"))
                     {
                           fprintf(fp2,"%s \n",lineBuf+10);
//parse( "" , "/home/shailendra/test.txt");
                        }
                    }
                    fclose(fp2);
                }
                fclose(fp);
            }   
        }
        return 0;
    }

Though i am still trying to use recursion to get inside include files also and get their include files written in the output file but didn't get successful.All the include files ate in the same directory.

structure will be like

include file 1 --> subinclude file 1 --> subinclude file 2 include file 2 --> subinclude file 1 -->sub include file 2 include file 3 include file 4 inlcude file 5

So till now i am able to get only include file 1, 2, 3, 4.I still have to use recursion to get all the subinclude file 1,2 etc.

shailendra
  • 271
  • 2
  • 6
  • 18
  • I was asking if you were successful in using recursion to get all of the sub-include files into the output file? I imagine that task will require use of functions like ***[FindFirstFile()](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx)*** and FindNextFile(). – ryyker Sep 16 '13 at 15:07
  • @ryyker: well i think that the same function would work with a condition that all the include files are in the same directory.If this is condition, with some edit to same function, it would work.I hadn't tried it yet, but of course i will do it today. – shailendra Sep 17 '13 at 06:35