-1

I am trying to write a program that imitates the functions of the C preprocessor. So my question is about expanding #include "header.h" statements.

I have a FILE * pointer to the source file, so I need to scan for all the header files included in the source file, and for each one, enter to the header file and copy its content to a new file, and than copy the original source file to the new file, which will result in an expanded header files program.

My problem: reaching to the actual header files by scanning the source file (this is just for headers I have written, so they should be looked for in the folder where the source file is).

Any ideas would be appreciated. (I haven't posted my miserable attempts, but if it will help somehow, I will post them).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ahdriam
  • 21
  • 4
  • 2
    Yes, please post your attempts, because this will show where exactly you got stuck. – anatolyg Jun 03 '15 at 18:24
  • There are plenty resources out there about how to read a line of text once you have a FILE pointer, as returned by fopen(). I would also recommend a book on C; cf. http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list with some free ones. – Peter - Reinstate Monica Jun 03 '15 at 20:24
  • It goes like this: Analyse problem. Devise solution. **Read documentation of your tools**. Implement solution. Test. – PJTraill Jun 05 '15 at 08:17

1 Answers1

2
  • You open your output file

  • You fgets line by line from your source file.

  • You check each line if it starts with #include. If no, copy the line read to your output. If yes, open the file whose name follows after the #include and copy its content to your output.

  • repeat until end of input.

The following is your preprocessor (I haven't tested it; it should work):

#define FALSE   0
#define TRUE    1
#define MAX_LINE 1024

char linebuf[MAX_LINE];
FILE *fpout;

int preprocessor(FILE *fpin);

int main(int argc, char **argv)
{
    FILE *fpin;
    if ((fpout=fopen(argv[2],"w"))==NULL) return(1);
    if ((fpin= fopen(argv[1],"r"))==NULL) return(1);
    preprocessor(fpin);
    fclose(fpin);
    fclose(fpout);
    return(0);
}
int preprocessor(FILE *fpin)
{
    FILE *fpin2;
    while (fgets(linebuf,MAX_LINE,fpin))
    {
        if (strncmp(linebuf,"#include",8)==0) {
            char *cp1, *cp2;
            if ((cp1= strchr(linebuf+9,'"'))==NULL) {fclose(fpin); return(FALSE);}
            if ((cp2= strchr(cp1+1,    '"'))==NULL) {fclose(fpin); return(FALSE);}
            *cp2='\0';
            if ((fpin2=fopen(cp1,"r"))==NULL) {
                printf("File '%s' not found.\n",cp1);
                return(FALSE);
            }
            if (!preprocessor(fpin2)) {fclose(fpin2); return(FALSE);}
            fclose(fpin2);
        }
        else fputs(linebuf, fpout);
    }
    return(TRUE);
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • The lines copied from the included file should be subjected to the same processing; includes are nested. – Peter - Reinstate Monica Jun 03 '15 at 20:17
  • Thanks for answering. How do I open the source file so I could read from it. I only know to use fopen with the file name, and I only got the FILE * pointer. – Ahdriam Jun 03 '15 at 20:32
  • 1
    @Ahdriam: if you have to ask that, the entire rest of this project of yours is most likely beyond your current skills. Perhaps you ought to start with something a bit easier. – Jongware Jun 03 '15 at 20:40
  • yes, I have started very recently but I think that after being able to read the source file, I can manage the rest, and I have never seen before an example of opening of a file with a pointer to it, just by its name, I tried difference things and I search for it on google, and I havn't find an example for that. – Ahdriam Jun 03 '15 at 20:58
  • This is a very basic question and you should read a book on C. But to answer: you give `fopen` the name of the file you want to open. It returns a pointer to a "thing" you use to tell `fgets` from what opened file to read. `FILE *fp=fopen("myFile", "r"); ... fgets(buf, bufsize, fp);...` – Paul Ogilvie Jun 04 '15 at 09:14
  • But the thing is I have a function which returns a file pointer, and another function gets this file pointer as a parameter, and I want the second function to open the file, which is recived closed, and I don't want to transfer the file name – Ahdriam Jun 08 '15 at 14:57
  • If you get a pointer to a file, that is, to a `FILE *` and it is closed, then you *cannot* re-open it. You either must give the file pointer as an open file, or give the filename. – Paul Ogilvie Jun 08 '15 at 15:21
  • oh, and I tried for so long to do it.... now, I didn't close the file and passed the pointer, and then I did: fscanf(file, "%s", string) where file is the pointer name, but instead of getting the first "word" in the file, string got the name of the file. Why is that? – Ahdriam Jun 08 '15 at 15:33
  • 1
    @Ahdriam, I changed the solution to adress your problem. I suggest you forget your own code and use the solution. Otherwise you must post yur code so we can study it and tell you all your "why's". – Paul Ogilvie Jun 08 '15 at 15:43