Go Down

Topic: Change FileName And Count NameIndex (Read 466 times) previous topic - next topic

donclemente

Hello together,
i´d like to Change Filename by pressing a button:
Messer00.csv
Logger00.csv etc...
and i want to Count the index up if the file is already exisiting

Messer00 - Messer01.csv etc


  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }


so, what to do - please
thanks
DonClemente

PaulS

Quote
so, what to do - please
That snippet of code seems to do what you want. What is the problem?
The art of getting good answers lies in asking good questions.

donclemente

i come from vb.net and i´m new on c
so i try again

in
char filename[] = "LOGGER00.CSV";

i want to replace
"LOGGER00.CSV" by some Kind of variable; lets call it FN

so it Looks something like
char filename[] = FN;



when pressing button 1 the FN changes
FN = Messer00.csv
FN = Logger00.csv

when pressing button 2 the saving begins...

  // create a new file
  char filename[] = FN;
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }

so, how to declare FN ?
Thanks DC

larryd

Create a new function, pass the function the address of the array you want to use as the file name.


.
No technical PMs.
The last thing you did is where you should start looking.

donclemente

thanks for your help,
but as im new on C could i please have some code...

larryd

#5
May 25, 2017, 11:03 pm Last Edit: May 26, 2017, 01:51 am by larryd
Read this to learn about functions:
https://www.arduino.cc/en/Reference/FunctionDeclaration

Create a function in which you pass a byte parameter, let's call it 'whichFile'.
When 'whichFile' is == 0, use Messer00.csv as the base for your file name.
When 'whichFile' is == 1, use Logger00.csv as the base for your file name.


Edit:

Code: (NOT TESTED) [Select]

//create a new file and open for write
//return true if successful, else false
bool openFile(byte whichFile)
{
  if (whichFile != 0 && whichFile != 1)
  {
    return false;
  }

  char filename1[] = "Messer00.csv"; //whichFile == 0  default
  char filename2[] = "Logger00.csv"; //whichFile == 1
  char *p = filename1;

  if (whichFile == 1)
  {
    p = filename2;
  }

  for (uint8_t i = 0; i < 100; i++)
  {
    p[6] = i / 10 + '0'; //  or     *(p + 6) = i / 10 + '0';
    p[7] = i % 10 + '0'; //  or     *(p + 7) = i % 10 + '0';

    if ( logFile.open( p, O_CREAT | O_EXCL | O_WRITE ) )
    {
      break;
    }
  }

  return logFile.isOpen();

} //E N D  o f   o p e n F i l e ( )



.
No technical PMs.
The last thing you did is where you should start looking.

Go Up