I am developing a pos application with c language,
What i am trying to achieve is to add, and delete users. Adding is not a problem at all. but deleting is becoming too tricky What i am doing is, i am seaching for the record using fread(), then as soon as i find it, I set the whole record into spaces and this part is doing fine, the problem is when i try to read that file as soon as i delete a record, i cant read past the deleted record but if i restart my POS machine i can read past the deleted record. Any Help

Dani AI

Generated

Short summary and likely causes: 's symptom — a deleted record overwritten with spaces blocks further reads until a restart — usually points to logic or I/O-state problems rather than the act of using spaces itself. As noted, a search routine that returns with no value is undefined behavior and can corrupt control flow. Other common causes are incorrect file-positioning (seeking from the wrong origin), ignoring return codes from open/seek/write/close, or filesystem/flash drivers that buffer or require an explicit commit.

Practical fixes to apply first (low effort, high payoff): make every function return and propagate meaningful error codes; check every API return (open/seek/read/write/close) and log failures; use absolute seeks when jumping to a record offset; verify the file position after the seek before reading or writing. Replace the “blank with spaces” delete strategy with an explicit deleted flag in the record header (a single byte) so reader code can skip tombstoned entries deterministically. If the platform's file/flash layer buffers writes, ensure a flush/commit call is used or that close actually commits before assuming data is durable.

Design suggestions and diagnostics: consider keeping an in-memory array of records and rewriting the whole table on changes (as suggested by ) for small fixed-size tables — that avoids complex seek logic. Add diagnostic output: record index, calculated byte offset, bytes written/read, and all API return codes. A hex dump of the file after a failed delete will quickly show whether the on-flash contents and offsets match expectations.

Caution: embedded flash filesystems can have driver-specific semantics (caching, erase granularity, atomicity). Fix the control-flow bug and add strict error checks first; most intermittent, “works after reboot” problems clear up once the code properly checks and commits the low-level I/O results.

Recommended Answers

All 5 Replies

Post your/some of your code

HIE BANFA

int DeleteWaiter(char * tmpWaiterNo)
{
    int retval, ndx, pos;
    char pin1[5], pin2[5],tmpWaiterName[10];
    memset(pin1, 0, 5);
    memset(pin2, 0, 5);

    ndx = findWaiterInFile(tmpWaiterNo);//this is working fine 
    if (ndx < 0)
    {
        return ndx;
    }

    strncpy(&WaiterInfo.wName[0], "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20", 10);
    strncpy(&WaiterInfo.wNumber[0], "\x20\x20\x20\x20\x20", 5);
    pos = ndx * sizeof(WAITER_RECORD);
    retval = writeFile("WAITERINFO", pos, &WaiterInfo, sizeof(WAITER_RECORD));
    if(retval < 0)
        return(retval);

    return 0;
}


int findWaiterInFile(char * waiterNo)
{
    int retval,ndx;
    for (ndx=0; ndx<MAX_WAITERS; ++ndx)
    {
        memset(&WaiterInfo,' ',sizeof(WAITER_RECORD));  
        retval = readWaiterRecord(ndx);
        if(retval<0)
        { 
            return;
        }
        if(memcmp(WaiterInfo.wNumber,waiterNo,5)==0)
        {
            return(ndx);
        }
    }
    return(-1);
}


int16 writeFile(char *filename, dword offset, void *data, word size)
{
    int16 retcode;
    uint32 fid;
    int32 filepos;

    // open the local file for reading/writing
    fid = FlashFileOpen(filename, (O_RDWR));

    // position at the given offset
    if(offset>0)
    {
        filepos = FlashFileSeek(fid, (int32)offset,SEEK_CUR );
    }
    else
        filepos = FlashFileSeek(fid, (int32)offset, SEEK_SET);

    // write to the file
    FlashFileWrite(fid, data, size);

    retcode = FlashFileClose(fid);

    return(0);
}


int16 readFile(char *filename, long offset, void *data, long length)
{
    int16 retcode;
    uint32 fid;
    int32 filepos;

    fid = FlashFileOpen(filename, (O_RDONLY|O_RDWR));
    if (fid < 0)
        return(fid);
    if(offset>0)
    {
        filepos = FlashFileSeek(fid, (int32)offset, SEEK_CUR);

    }
    else
        filepos = FlashFileSeek(fid, (int32)offset, SEEK_SET);
    retcode = FlashFileRead(fid, data, length);

    retcode = FlashFileClose(fid);

    return(0);
}

At line 34 you should be returning a value, -1?

What does readWaiterRecord do?

readWaiterRecord reads the waiter record that is present at index "ndx". it only returns -1 when file to read is not present. here its returning 0 since its finding the waiter file.

I recommend you to use binary file read write for POS applications. Create an array of WaiterInfo and re-write all data to file every time that fields changed. Dont append to file or something like that. In this way you must add to or delete from array then write whole array to file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.