how to save reading data on sd file and compare.

hi,
i'm new to this arduino world and i have a project for may class. i have an arduino mega 2560, high precision RTc sd+eth shield and wiegand keypad with rfid incorporated. i'm 90% done and working, the only problem i cant solve it right now, is that i dont know how to manage data very well. let me explain to you.

--i have 4 rfid cards
--i need to enroll 3 of them in the system, the card codes need to be saved in a file but i cant save the same code twice or more.
--the file is text
--when i aproach the rfid cards to the reader, i need to check the card code if its in the system
--if the code is corect i open the gate and log that card

my problem is:
--how do i save the card code only once?
--how do i delete that code if i want to remove it from the system?
--how do i check if the code is in the system?

i need help, any ideea pls

thanks.

--how do i save the card code only once?

You read the whole file. If the data is not there, you add it. If it is, obviously you don't need to.

--how do i delete that code if i want to remove it from the system?

"Code" is a poor choice of words. "Data" would be better. If the records are fixed length, deleting a single record is easy. Just move the rest up one position. If not, it's a bit harder.

Since you don't have a file yet, seriously consider fixed length records. It will make reading much easier, too.

Deleting data from a file requires opening two files, reading from one and writing, or not writing, as appropriate, to the other one. Close both. Delete the first file, and rename the second file.

If you use fixed length files, then you don't really need to delete a record. Simply replace the data with a sentinal that means "this record not used".

If your file contains:

12345678
-1
87654321

and -1 means "not used" then you could print the records that are not not used.

You could add a record, 55667788, at the location that holds -1 to get

12345678
55667788
87654321

When the files contains no -1 records, and you need to add a record, 22334455, add it at the end:

12345678
55667788
87654321
22334455

If you want to delete the record 55667788, simply replace it with a -1:

12345678
-1
87654321
22334455

--how do i check if the code is in the system?

That depends a lot on how you store the data - binary or ASCII, fixed length or variable length, as char arrays or as (crappy) Strings, etc.

hi,
thank you very much for your answers.

i would like to provide aditional info for this:

That depends a lot on how you store the data - binary or ASCII, fixed length or variable length, as char arrays or as (crappy) Strings, etc.

I read the rfid code of the card (data if you like, i'm not native english speaker). the data(code) is a number, i read it in DEC and store as LONG variable.some cards have a 5 figure code others 7 figure code or more. and save it in a file, each data(code) on a different row.

thank you again

I read the rfid code of the card (data if you like, i'm not native english speaker). the data(code) is a number, i read it in DEC and store as LONG variable.some cards have a 5 figure code others 7 figure code or more.

I'd need to see the code that does this. Most RFID readers return the tag ID as a string. The string is fixed length, usually 10 characters.

Hi,
this is my code used to read a rfid card.

long codRfid = 0;
int gateCode = 0;

void rfidRead() {
  if (wg.available())
  {
    codRfid = wg.getCode();
    gateCode = wg.getGateActive();
    Serial.println(codRfid);
  }
}

this is my code used to read a rfid card

Well, some of it, anyway. So, here is some of the answer.

I'd plan on creating a binary file, so the records are fixed leng...

i use wiegand library. this is the procedure i use to read the card. and then i write it to a csv file.