LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Auto increment an alphanumeric ID in C (https://www.linuxquestions.org/questions/programming-9/auto-increment-an-alphanumeric-id-in-c-727720/)

PMR 05-22-2009 01:43 AM

Auto increment an alphanumeric ID in C
 
Hi everybody..
Can anyone give me any ideas as to how i can auto increment an alphanumeric number? I have files which have ID's of the format - F0001, F0002,....,F0100. Now i have pre existing files till F0008, and when new files are to be added, it must detect the last file ID(which in our case is F0008), increment it and then save the new file with the new ID.

I hope someone can help me soon..

Thanks...

soleilarw 05-22-2009 08:42 AM

Using printf formatting code will help you to create the names automatically with leading zeroes. In a similar way you could use system ("ls -1 ./F* > outfile") to gather a list of the relevant files, and then use fopen and fgets and strtok to wade through the contents.
You will certainly need some good documentation to proceed.

Linux Archive

PMR 05-22-2009 10:40 AM

Thanks..but since am new to this, I am finding it a little hard to understand your point..could you be a little more elaborate so that I can have a clear understanding???

fantas 05-22-2009 11:08 AM

If you are the only reader/writer you could use a function like scandir which gives you a sorted list of the directory entries and then you should be able to work your way from there on.

An other idea could be to store a tiny hidden file in the directory which only contains the last value of the index, and only use this to read/update the current index. If there are more than one reader/writer this could be used as a synchronisation primitive of sorts as well.


All times are GMT -5. The time now is 01:21 AM.