Incrementing file names

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I searched but didn't find any sort of meaningful answer, can you help guys?

I am trying to load from an SD card, graphic data into the on board serial flash memory files for a 7" CTE LCD display.

all goes well 0 to 9 but then will not increment to 10.

the File names are 0.CTE 1.CTE ... and so on.

the 10th increment looks for a file called :.CTE (I think that's because that's the character next in the ASCII table)

if I call for 10.CTE the next increment is 20.CTE!

is this a well know gotcha? and is there a work around

IMAGE_FILENAME[0]=IMAGE_FILENAME[0]++;

this is how it increments.

Regards
Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"9 + 1 is :" in the micro world.. In the code treat the numbers always as integers.. I would suggest having something like this

int file_name = 10;
//convert it to ascii and access the *.CTE file
access_cte_file(itoa(filename));

You can even write a manual routine for that..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More information is required, specifically, the code that constructs the filenames n.CTE using what is in array IMAGE_FILENAME[]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd just sprintf() to create the filename. You can then do stuff like leading zero padding.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One day, I really need to learn to use sprintf.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's just printf() into a buffer ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I may be talking cross purposes here but each file is created in GIMP a graphics program and as such named by me when I save the file. Each file is a jpg icon which is then converted to a different format and again named by me when saved as the .CTE file.

Its the SD to serial memory transfer that's not getting past the 9.CTE file!

attached file is the code (arduino)

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah but the reason is obvious isn't it? What do you think '9'+1 is? If you answered "10" you don't understand ASCII / C.

As I say sprintf is the obvious answer here. Some people will say sprintf/printf uses too much flash space. But you are already doing SD/FAT which already costs 20-30K so the odd extra 1-2K doesn't really matter does it?

Apart from anything else:

char IMAGE_FILENAME[] = "0.CTE";

where does "10.CTE" fit into the storage allocated for that string?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes you are correct I don't understand C or ASCCI, I mean how does the string "0.CTE" plus 1 equal "1.CTE"

why would it increment the 0 and not any other part of the string? why just the left hand char?

Thanks for the sprint pointer I will go read

Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
But you are already doing SD/FAT which already costs 20-30K so the odd extra 1-2K doesn't really matter does it?

once the images are on the LCD I reprogram the Arduino to a different program and use UTFT library to access the stored icons and fonts so save memory and speed that way though, im not sure how much of either.

Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nigelf wrote:
why would it increment the 0 and not any other part of the string? why just the left hand char?
If you were using this
IMAGE_FILENAME[0]=IMAGE_FILENAME[0]++; 

it is because you told it to.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char IMAGE_FILENAME[] = "0.CTE";

sets the size of that array to 6 bytes, so that will limit you to a single digit filename 0 .. 9

Quote:
why would it increment the 0 and not any other part of the string? why just the left hand char?
Because the string is just a sequence of bytes, changing one byte does not modify the other bytes.

sprintf() will probably be the simplest method, eg

char IMAGE_FILENAME[7];
int next_filenumber = 0;
...
sprintf( &IMAGE_FILENAME[0], "%d.CTE", next_filenumber );   /// create the filename
...
filenumber ++;   // increment to next file

which will let you do 0 .. 99

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

why just the left hand char?

Ummm--because >>your code<< told it to do that?

Quote:

IMAGE_FILENAME[0]=IMAGE_FILENAME[0]++;

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok I admit I am real slow! thanks guys I just made the connection it says increment the 0 part of the string. dam ! again thanks.

.... im off to eat some humble pie :)

Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm a little surprised that nobody has mentioned that this code exhibits undefined behavior:

IMAGE_FILENAME[0]=IMAGE_FILENAME[0]++;

You cannot modify the same object more than once between sequence points. In this case, does the object get modified by the increment first or by the assignment first? The C standards don't say, and the compiler can do anything with that statement because it's undefined. A different compiler, or a different version of the same compiler, or even simply different compile options, may result in different behavior.

Of course, that's moot if you use a different method to increment your file names.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just look at an ascii chart. Each character is represented by a number. '9' is 57. Add 1 to 57 to get 58, and that's a colon ':'.

http://www.asciitable.com/

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead.