fx_file_write - Adding to an existing file.

Lets say for example file1.txt has lines

Line 1    1,2,3\r\n

Line 2    1,2,3\r\n

 

I want to change  '3' in first line to "33". So, I have set the file pointer appropriately and using fx_file_write to make this change and it looks like:

Line 1    1,2,33\r\n     (Change as expected)

Line 2    ,2,3\r\n       (Loosing first byte in this line)

 

Why would line 2 loose the first byte during this process ? and as you keep adding stuff to line 1, the contents on the next line gets erased...

 

SSP 1.6.3

Thanks,

Sam

 

  • Hi Sam,

    If you set the file pointer to anywhere in the file except the end, you will overwrite data in the file. This is consistent with the fseek/fread/fwrite operations on linux or a PC. If you wish to insert data without loosing data in the middle of the file, you must handle this in your application by either reading the file into memory, manipulating the data in memory and then rewrite the file from the insertion point or copy the file into a new file and insert the data while the copy is being made.

    -Gary
  • How are you calling fx_file_write? And are you looking at the file data in memory or after flushing and closing the media (you are flushing the media, correct)?

    I did a similar test on FileX in 1.6.3. I added the '# ' character to the 6th position in the following test.txt file:

    1,2,3\r\n
    a, ,b,c\r\n

    And the result was :
    1,2,3\r
    #a,b ,c\r\n

    I wrote repeatedly to the same position in the file with no change in the result. If I advance the index for 4-5 places I get

    1,2,3\r 0x312c322c330d
    ####c\r 0x23232323630d

    The overwriting of the line you are seeing would be the line carriage return in \r\n being overwritten. But are you writing to the same place on repetitive write or moving the offset in fx_file_write?

    Janet