SD CARD! I can't read and write a file for two or more times!!!

Hi have a problem with my SD card.
(PREMISE: I have formatted SD as low level and I tried many sd cards but the problem is the same. I have an Arduino Uno with an ethernet shield and a SD card formatted like FAT32)

I was to trying to read and write a file .xml on my SD card when I noticed that:
reading and writing several times with the my same functions(readfile and writefile) the data were unpacked on corrupted!!!

why?????

The strange things is that if I call for the first time these functions(readFile and writeFile) I have the expected results, but if I try to recall the same functions the file xml is unpacked ( i have lost old message present in the sd card!)

Please, help me :confused: :frowning:

sketch_oct20c.ino (1.78 KB)

I ran your code on a Teensy3.1 and it works just fine. I suspect that you are running into a problem with the small amount of sram available on the UNO. It only has 2kb whereas the T3.1 has 64kb. The String library is almost certainly the cause of the problem. Try using a C-style null-terminated string (char array) instead of the String.
Or, you might be able to find a way to use the String class more efficiently. I don't think it helps that you read the file one char at a time and append it to a String.

Pete