C Preprocessor -- processing file name

WBahn

Joined Mar 31, 2012
26,398
If I use the __FILE__ macro it expands to the name of the current file as a string constant.

What I want to do is define a macro to be the bare file name.

For instance, I might have a file FRED.c in the directory C:\projects. If I have

#define FILENAME __FILE__

I get FILENAME defined as the string constant "FRED.c" (I think some compilers return the path information as well, mine doesn't (at least by default)).

What I need is to be able to set FILENAME to just FRED (without the extension and without the quotes).

Any ideas on how this might be done? It needs to be done with the preprocessor, so writing a function isn't an option. Nor is writing a separate program to read this file and modify the code before compiling it.

nsaspook

Joined Aug 27, 2009
8,239
Sub String? That would be a neat trick. I don't know if you can abuse the C preprocessor to do that but it's possible to run another preprocessor step after with something like 'perl/M4' using Make to modify the __FILE__ constants (with a token sequence) to something else in the C source file.

This is what you normally get.

file lo.c RPI2 gcc
root@rpimedia:~# make lo
cc lo.c -o lo

C:
#include <stdio.h>

int main ()
{
printf ("This is line %d of file \"%s\".\n",
__LINE__, __FILE__);
return 0;
}

root@rpimedia:~# ./lo
This is line 6 of file "lo.c".

Last edited:

WBahn

Joined Mar 31, 2012
26,398
I'm definitely of the opinion that what I want to do can't be done. Which is a shame, but not the end of the world.

The reason I would love to be able to do it is because I write "pseudo classes" in C and use macros to autogenerate much of the code. The code is keyed to the class name via a macro

#define CLASS FRED

and the file is named FRED.c

So if I could get at the base name of the file I could rename a file and, in doing so, rename the class throughout the entire file. As it is, I have to go into the file and change that one #define at the top of the file, so it isn't a big hassle.

Brownout

Joined Jan 10, 2012
2,390
I just use a stream editor.

WBahn

Joined Mar 31, 2012
26,398
I just use a stream editor.
Hello... "It needs to be done with the preprocessor, so writing a function isn't an option. Nor is writing a separate program to read this file and modify the code before compiling it."

That rules out using sed or any other stream editor. Besides, I'm not writing code that someone that doesn't know what a stream editor is, or how to use one, can't use. That completely defeats the purpose.

Brownout

Joined Jan 10, 2012
2,390
Still an improvement over the process you described. And you've already said you can't do it the way you wanted.

Last edited:

WBahn

Joined Mar 31, 2012
26,398
How is it an improvement over the process I am currently using?

Current process: If you want to change the name of the class you have to do two things:
1) Change the name of the file.
2) Change the value of a single #define statement at the top of the file.

What I was hoping to do was eliminate that second step, but that step is definitely not a big hassle. It is comparable to what you would need to do to change the name of a class in Java.

Actually, unlike Java, the class name and the file name do not have to be the same, so all you really have to do is step two. I just strongly recommend that they be named the same from an organizational perspective.

How is adding the complication of having to write and debug a stream editor script and then ensure that anyone that uses my code has that same stream editor and jumps through the hoops of running it whenever they change the name of the class an improvement over opening the file and changing one word in it?

Brownout

Joined Jan 10, 2012
2,390
Stream editors can be used directly from the command line. No need to write and debug a script. Actually, there are many ways to eliminate the 2nd step automatically, which are even more elegant. They require a simple script to be written once and used many times,typically a single line of code. That's an investment I have no problem making.

WBahn

Joined Mar 31, 2012
26,398
I know stream editors can be used from the command line. That doesn't change the fact that not everyone that uses my macros are going to even have a stream editor, let alone the particular stream editor that I've supplied a script for. The very fact that they might think that they even need a stream editor will scare some folks off.

Brownout

Joined Jan 10, 2012
2,390
I consider this an improvement whether your users are sacred or not. When I made the suggestion, you were talking about how YOU made the changes:

As it is, I have to go into the file and change that one #define at the top of the file, so it isn't a big hassle
If you want something so that someone else can change the definitions, then that's something else.

NorthGuy

Joined Jun 28, 2014
611
For many compilers it wouldn't matter if you renamed "fred.c" to "fred". So, if you're not afraid of loosing c at the end of your filename, it'll be just what you wanted.