Go Down

Topic: Change one character in an SD file? (Read 115 times) previous topic - next topic

heinburgh

I have a reference file on an SD card that holds around 1000 records that look like this:

Code: [Select]
982000421306246,0,B112,BW,0,!
982000421306248,0,D052,BW,0,!
982000421306249,0,B467,BW,0,!
982000421306251,0,A239,BW,0,!
982000421306252,0,A111,BW,0,!
982000421306253,0,A099,BW,0,!


The second column needs to chage from 0 to 1 as soon as that specific record has been read. From looking through the forum it seems so far that I can only do this by copying the entire file to a temporary file with the change added, deleting the original, and renaming the new file to the original file name.

The application is restrained by time, as the counting needs to happen fast so this is not an ideal solution. Is there a quicker / easier way? In my ignorance I did a tempFile.seek() and tempFile.write('1') but this is obviously not how it works.

arduarn

I would have expected the seek/write method to work; why do you think it doesn't?.
Depending on what you are doing, you may not even need the temporary file. Perhaps it is best if you post your code so we can get a better idea of what you are doing.

heinburgh

This is what I tried, with no visible results. tempFile is successfully opened and closed from within the function that calls checkMatchandAssignData(), hence it is not present here.



Code: [Select]
void checkMatchandAssignData()
    {
      counted = '\0';
      if (strcmp(EID, eidtest) == 0)      // CHECK FULL TAGNUMBER IN TEMP FILE AGAINST CURRENT EID
           {
             Serial.println(F("MATCH FOUND!!!"));
             byte dump = tempFile.read();         // remove comma after EID in tempfile

             byte duplicate = tempFile.read();   
             if (duplicate == '0')                // if EID has not been counted in session -
             { 
               Serial.println(F("<<< FIRST READ!!! >>>"));
               int duppos = tempFile.position();
               Serial.print(F("dup position = "));
               Serial.println(duppos);
               tempFile.seek(duppos - 1);           
               tempFile.print('1');               // change duplicate as "EID already read"
               match = true;
               blueStart = millis();
             }
             else if (duplicate == '1')
             {
               duplicateBuzz();
               Serial.println(F("<<< DUPLICATE READ!!! >>>"));
               return;
             }
               
           }
           else if (strcmp(EID, eidtest) != 0)
           {
             y = 0;
             eidtest[y] = '\0';
             vidtest[0] = 'N';
             vidtest[1] = 'O';
             vidtest[2] = ' ';
             vidtest[3] = 'M';
             vidtest[4] = 'A';
             vidtest[5] = 'T';
             vidtest[6] = 'C';   
             vidtest[7] = '\0';
             hok[0] = 'H';
             hok[1] = '!';
             hok[2] = '\0';
             draft = '0';
             fulltag = false;           
           } 
    }

arduarn

Unfortunately when you don't post all the code it results in lots of little unanswered questions, like - did you open the file for writing or just reading?

I think you might be better with the write() function instead of print(), and print the return values of write() and seek() to serial for debug purposes.

heinburgh

Ahaa..   tempFile = SD.open(tempFilename, FILE_READ);

So it is open for read only. Can one open a file for read and write at the same time?

I had it as tempFile.write, but when I had no results I tried print... and that's where it stayed.

aarg

Ahaa..   tempFile = SD.open(tempFilename, FILE_READ);

So it is open for read only. Can one open a file for read and write at the same time?

I had it as tempFile.write, but when I had no results I tried print... and that's where it stayed.
When you open it as write, it can be read and written. You have fixed length records, so you can perform random access to the file and modify records without creating another file.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

arduarn

So it is open for read only. Can one open a file for read and write at the same time?
Of course.

heinburgh

Got it. She is purring like a kitten. All I needed was file.write instead of file.read.

Thanks all.

Go Up