Shop OBEX P1 Docs P2 Docs Learn Events
Datalogger modify a Text file — Parallax Forums

Datalogger modify a Text file

Michael @ AfineolMichael @ Afineol Posts: 33
edited 2011-08-04 23:47 in Propeller 1
I can now open a text file, read its contents and then write those contents to another text file. What I need to be able to do now is read a single character from a text file say "0". Store in a var, Increament it, and then overwrite "0" with "1". I tried the following code:
  USBOffset := 0  
  IF logger.openFileForWrite(string("TestUP.txt"),0)
    logger.seek(USBOffset)                                                          
    logger.readFromFileUntilChar(string("TestUP.txt"),@arrUSBBuffer,CR,@USBOffset)
    logger.seek(0)
    TestFile := arrUSBBuffer[0]
    TestFile++
    logger.writeToFile(@TestFile,1,0)
    logger.closeFile(string("TestUP.txt"))

Before the code runs the TestUP.txt file contains: 30,0D,0A ("0CRLF")

After the code my TestUP.txt file now contains: 31 ("1") The CRLF is missing.

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-08-04 23:47
    It looks like your file is being read into a buffer, then TestFile is set to the first value held in the buffer, incremented, and then only this value is being written back to the file. So you're dropping the CRLF in the process.

    My Spin skills are pretty weak, so this is more psedocode than anything, but see if this idea makes sense...
      USBOffset := 0  
      IF logger.openFileForWrite(string("TestUP.txt"),0)
        logger.seek(USBOffset)                                                          
        logger.readFromFileUntilChar(string("TestUP.txt"),@arrUSBBuffer,CR,@USBOffset)
        logger.seek(0)
        TestFile := arrUSBBuffer[0] ' set TestFile to 30... 
        ' TestFile++
        arrUSBBuffer[0] := TestFile++ ' set arrUSBBuffer[0] to TestFile++ - now 31
        ' logger.writeToFile(@TestFile,1,0)
        logger.writeToFile(@arrUSBBuffer,?,?) ' instead of writing byte TestFile to file, write buffer arrUSBBuffer
        logger.closeFile(string("TestUP.txt"))
    
    I commented out the lines I changed so you can see the difference. I'm not sure of the syntax on writeToFile, so I left place holders.
Sign In or Register to comment.