File Seek And Write

Home Forums mDot/xDot File Seek And Write

Tagged: , ,

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #14608
    Geoff Field
    Participant

    Hi,

    I’m working on a project using the emBitz IDE and the mDot library on an STM32F411 mBed platform. I have a task to turn a flat log file into a FIFO file. At full stretch, this file will take up most of the available 2MB.

    What I’d like to be able to do is seek to a specific location and overwrite the location. My existing code is as follows:


    // Obviously, mdot.h is included and a global object gMdotPtr points at the library
    mDot::mdot_file file = gMdotPtr->openUserFile(FILE_NAME, mDot::FM_RDWR);
    if (file.fd >= 0) {
    // Write the entry at the required location
    bool result = gMdotPtr->seekUserFile(file, mRecordLocation, SEEK_SET);
    if (result) {
    result = gMdotPtr->writeUserFile(file, (void *)pLogRecord, sizeof(T_LogEntry));

    // Move on to the next location
    mRecordLocation += sizeof(T_LogEntry);
    if (mRecordLocation >= mCurrentSize) {
    mRecordLocation = 0;
    }
    }
    gMdotPtr->closeUserFile(file);
    }

    What I’m seeing is that the file write is failing – at least, it’s returning a false value and the file contents are unchanged.

    Any ideas?

    • This topic was modified 7 years, 8 months ago by Geoff Field. Reason: Incorrect code format
    #14611
    Mike Fiore
    Blocked

    Geoff,

    I wrote up a simple app to test this scenario. It creates a 1kB file and seeks to a few random positions and overwrites the original data. It then prints out the address it overwrote and the entire file so the operations can be verified. Here’s the debug from the test on my device:

    
    [INFO] version: 2.0.3
    [INFO] deleting user files
            deleting test_file.txt [1024]
    [INFO] creating 1kB file of 0x3B
    [INFO] seeking to 0x03C0
    [INFO] writing 8 '0xA2' bytes to 0x03C0
    [INFO] seeking to 0x039B
    [INFO] writing 8 '0xA2' bytes to 0x039B
    [INFO] seeking to 0x0191
    [INFO] writing 8 '0xA2' bytes to 0x0191
    [INFO] seeking to 0x0090
    [INFO] writing 8 '0xA2' bytes to 0x0090
    [INFO] seeking to 0x00C7
    [INFO] writing 8 '0xA2' bytes to 0x00C7
    [INFO] seeking to 0x00CD
    [INFO] writing 8 '0xA2' bytes to 0x00CD
    [INFO] seeking to 0x0058
    [INFO] writing 8 '0xA2' bytes to 0x0058
    [INFO] seeking to 0x008F
    [INFO] writing 8 '0xA2' bytes to 0x008F
    [INFO] seeking to 0x0303
    [INFO] writing 8 '0xA2' bytes to 0x0303
    [INFO] seeking to 0x002C
    [INFO] writing 8 '0xA2' bytes to 0x002C
    [INFO] file data:
    0x0000  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0010  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0020  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B A2 A2 A2 A2 
    0x0030  A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0040  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0050  3B 3B 3B 3B 3B 3B 3B 3B A2 A2 A2 A2 A2 A2 A2 A2 
    0x0060  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0070  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0080  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B A2 
    0x0090  A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 
    0x00A0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x00B0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x00C0  3B 3B 3B 3B 3B 3B 3B A2 A2 A2 A2 A2 A2 A2 A2 A2 
    0x00D0  A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x00E0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x00F0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0100  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0110  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0120  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0130  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0140  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0150  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0160  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0170  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0180  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0190  3B A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 
    0x01A0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x01B0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x01C0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x01D0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x01E0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x01F0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0200  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0210  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0220  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0230  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0240  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0250  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0260  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0270  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0280  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0290  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02A0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02B0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02C0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02D0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02E0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x02F0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0300  3B 3B 3B A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 
    0x0310  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0320  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0330  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0340  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0350  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0360  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0370  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0380  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x0390  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B A2 A2 A2 A2 A2 
    0x03A0  A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x03B0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x03C0  A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 
    0x03D0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x03E0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    0x03F0  3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 
    

    I published the test application to my account. Feel free to try it yourself if you’d like.

    https://developer.mbed.org/users/mfiore/code/mDot_file_seek/

    Cheers,

    Mike

    #14613
    Geoff Field
    Participant

    Hi Mike,

    Thanks for the quick response.

    Your code worked correctly with my version of the library. See the PuTTY log below.

    I note that the mDot library has moved on by numerous revisions since the version I have on my system. I’m told by the original developer of the system that a newer version broke things, but I might try updating anyway. I did have problems compiling the project as stored in the Mercurial repository, mostly caused my current ignorance of how to set up to compile the device on the EmBitz platform. I’ll work that out, probably by the last resort of reading the manuals…

    Regards,

    Geoff

    [INFO] version: 0.0.9-1-ge7286ba
    [INFO] deleting user files
    deleting test_file.txt [1024]
    [INFO] creating 1kB file of 0x3B
    [INFO] seeking to 0x0025
    [INFO] writing 8 ‘0xA2’ bytes to 0x0025
    [INFO] seeking to 0x00B7
    [INFO] writing 8 ‘0xA2’ bytes to 0x00B7
    [INFO] seeking to 0x0366
    [INFO] writing 8 ‘0xA2’ bytes to 0x0366
    [INFO] seeking to 0x01D1
    [INFO] writing 8 ‘0xA2’ bytes to 0x01D1
    [INFO] seeking to 0x02FC
    [INFO] writing 8 ‘0xA2’ bytes to 0x02FC
    [INFO] seeking to 0x016C
    [INFO] writing 8 ‘0xA2’ bytes to 0x016C
    [INFO] seeking to 0x0178
    [INFO] writing 8 ‘0xA2’ bytes to 0x0178
    [INFO] seeking to 0x03B0
    [INFO] writing 8 ‘0xA2’ bytes to 0x03B0
    [INFO] seeking to 0x0278
    [INFO] writing 8 ‘0xA2’ bytes to 0x0278
    [INFO] seeking to 0x00C7
    [INFO] writing 8 ‘0xA2’ bytes to 0x00C7
    [INFO] file data:
    0x0000 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0010 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0020 3B 3B 3B 3B 3B A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B
    0x0030 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0040 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0050 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0060 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0070 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0080 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0090 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x00A0 3B 3B 3B 3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x0300 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0310 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0320 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0330 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0340 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0350 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0360 3B 3B 3B 3B 3B 3B A2 A2 A2 A2 A2 A2 A2 A2 3B 3B
    0x0370 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0380 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x0390 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x03A0 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x03B0 A2 A2 A2 A2 A2 A2 A2 A2 3B 3B 3B 3B 3B 3B 3B 3B
    0x03C0 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0x03D0 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B 3B
    0

    #14614
    Geoff Field
    Participant

    I have now worked out what was going wrong with my code. It was mostly rookie mistakes.
    First, the block-scope “result” variable overwrote the function-scope “result” variable, resulting in incorrect error reporting.
    Secondly, I had issues reading the log reliably. Once I had the log dump working correctly and reporting the offsets, I was able to see exactly what was going on.
    Finally, in the last version I tried I had set the “FM_APPEND” flag (out of desperation) so it was appending to the log regardless of what I did.

    Once I had fixed up all of the above, my problems went away.

    Thanks to Mike in particular for help with confirming that the functionality actually worked.

    Geoff

    #14615
    Mike Fiore
    Blocked

    Geoff,

    Glad to hear you’re up and running!

    I noticed that including FM_APPEND in the open mode caused a write after set to always append to the end of the file. I apologize, I should have mentioned that in my first post.

    With respect to any previous issues with the library or any new ones you find, please let us know and we’ll try to help you out.

    Thanks for choosing MultiTech!

    Cheers,

    Mike

    #14629
    Geoff Field
    Participant

    Thanks Mike,

    The FM_APPEND flag wasn’t in the version I posted, so there was no reason to mention it.

    I just wish there was more documentation on some of the functions, such as what the return values are, what the flags actually do, etc.

    Oh yes: one last rookie error.
    If you look at my original code, you’ll note that I’m reading the result of writeUserFile() into a Boolean.

    You’d never think I’d been writing software for 30 years…

    Regards,

    Geoff

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.