But my sketches are in a deep subfolder of my Dropbox folder so I always have to scroll horizontally to read the short sketch name. Can anyone suggest an edit to that Serial.println( F(__FILE__) );
line which would delete say the first 100 characters please?
Thanks but couldn't get any version of that working.
const char * compileFilename = __FILE__;
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println("=============================================");
Serial.print("Full path : ");
Serial.println(compileFilename);
// look for the last /
const char * lastSlashPtr = strrchr(compileFilename, '/'); // Orig
// https://en.cppreference.com/w/cpp/string/byte/strrchr
if (lastSlashPtr != nullptr)
{
Serial.print("File name : ");
Serial.println(lastSlashPtr);
}
}
void loop()
{
}
/*
Output:
Full path : C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test\JML-Test.ino
No short version printed
*/
const char * compileFilename = FILE;
I am using Windows so then tried changing '/' to ''. The IDE didn't like that!
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println("=============================================");
Serial.print("Full path : ");
Serial.println(compileFilename);
// look for the last \
const char * lastSlashPtr = strrchr(compileFilename, '\');
// https://en.cppreference.com/w/cpp/string/byte/strrchr
if (lastSlashPtr != nullptr)
{
Serial.print("File name : ");
Serial.println(lastSlashPtr);
}
}
void loop()
{
}
/*
Weird format! (Ctrl +T no change)
ERROR
'lastSlashPtr' was not declared in this scope
*/
Using double quotes didn't help either.
Also tried adding the #include statements from the article you referenced:
#include #include
Or this, which is my normal syntax? #include <iostream.h> #include <cstring.h>
But neither were accessible in the IDE.
Didn't really follow your "of course you could make it shorter"? By 'it' did you mean the code, not the printed result? FWIW I tried this without success:
That gave the error
'lastSlashPtr' was not declared in this scope
const char * compileFilename = __FILE__;
void setup() {
Serial.begin(115200);
Serial.print("Full path : "); Serial.println(compileFilename);
// look for the last \
const char * lastSlashPtr = strrchr(compileFilename, '\\'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
if (lastSlashPtr != nullptr) {
Serial.print("File name : "); Serial.println(lastSlashPtr);
}
}
void loop() {}
Here's the latter part of the full error report; let me know if you need more.
C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino:6:3: warning: multi-line comment [-Wcomment]
// look for the last \
^
C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino: In function 'void setup()':
JML-Test-1a:8:7: error: 'lastSlashPtr' was not declared in this scope
if (lastSlashPtr != nullptr) {
^~~~~~~~~~~~
C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino:8:7: note: suggested alternative: 'strcasestr'
if (lastSlashPtr != nullptr) {
^~~~~~~~~~~~
strcasestr
exit status 1
'lastSlashPtr' was not declared in this scope
lol forgot that an antislash at the end of the line tells the compiler to continue as if there was no break... hence the greyed out definition of lastSlashPtr which is part of the comment now
const char * compileFilename = __FILE__;
void setup() {
Serial.begin(115200);
Serial.print("Full path : "); Serial.println(compileFilename);
// look for the last antishlash '\' in our file name
const char * lastSeparator = strrchr(compileFilename, '\\'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
if (lastSeparator != nullptr) {
Serial.print("File name : "); Serial.println(lastSeparator);
}
}
void loop() {}