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

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);
}

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.

This article has been dead for over six months. Start a new discussion instead.