'Removing' the oldest line/row from a .CSV file

Posted By: Grant

'Removing' the oldest line/row from a .CSV file - 09/07/17 23:25

Greetings all,

I'm working on a way to 'remove' the oldest line/row from a .CSV file after adding a new line, using the file_append() function during each bar-interval. I say 'remove' between apostrophes because I understand that I need to copy the full file content to another file without including the line/row that I want to 'remove'. To accomplish this I found the following sample code, that I want to adjust for my own needs:

Code:
#include <stdio.h>
#include <stdlib.h> /* for atoi() and malloc() */
#include <string.h> /* for memmove() */
 
/* Conveniently print to standard error and exit nonzero. */
#define ERROR(fmt, arg) return fprintf(stderr, fmt "n", arg)
 
int main(int argc, char **argv)
{
    FILE *fp;
    char *buf;
    size_t sz;
    int start, count, lines = 1;
    int dest = 0, src = 0, pos = -1;
 
    /* Initialization and sanity checks */
    if (argc != 4)
        ERROR("Usage: %s <file> <start> <count>", argv[0]);
 
    if ((count = atoi(argv[3])) < 1) /* We're a no-op. */
        return 0;
 
    if ((start = atoi(argv[2])) < 1)
        ERROR("Error: <start> (%d) must be positive", start);
 
    if ((fp = fopen(argv[1], "r")) == NULL)
        ERROR("No such file: %s", argv[1]);
 
    /* Determine filesize and allocate a buffer accordingly. */
    fseek(fp, 0, SEEK_END);
    sz = ftell(fp);
    buf = malloc(sz + 1);
    rewind(fp);
 
    /* Fill the buffer, count lines, and remember a few important offsets. */
    while ((buf[++pos] = fgetc(fp)) != EOF) {
        if (buf[pos] == 'n') {
            ++lines;
            if (lines == start) dest = pos + 1;
            if (lines == start + count) src = pos + 1;
        }
    }
 
    /* We've been asked to do something weird; clean up and bail. */
    if (start + count > lines) {
        free(buf);
        fclose(fp);
        ERROR("Error: invalid parameters for file with %d lines", --lines);
    }
 
    /* Overwrite the lines to be deleted with the survivors. */
    memmove(buf + dest, buf + src, pos - src);
 
    /* Reopen the file and write back just enough of the buffer. */
    freopen(argv[1], "w", fp);
    fwrite(buf, pos - src + dest, 1, fp);
 
    free(buf);
    fclose(fp);
    return 0;
}


(source: https://www.rosettacode.org/wiki/Remove_lines_from_a_file#C)


One of the first problems I'm having, is that the memmove() function needs the <string.h> header, which isn't(?) supported by Zorro.

Is there a workaround for this function?

Thanks for any feedback!

Grant
Posted By: jrath

Re: 'Removing' the oldest line/row from a .CSV file - 09/07/17 23:48

That header file you found looks like it is C but Zorro is Lite-C. So not compatible.
Posted By: Grant

Re: 'Removing' the oldest line/row from a .CSV file - 09/08/17 11:18

Hello Jrath,
I understand that I need to modify multiple parts of the code in order to make it lite-c compatible. Replacing the memmove() is just one of the first issues.

Grant
Posted By: AndrewAMD

Re: 'Removing' the oldest line/row from a .CSV file - 09/08/17 15:13

Just wrap this code in a DLL and import the functions you need. (Better yet, get the C++ version.)

Otherwise, write a code from scratch that parses the entire file and overwrites it with all of the lines except the line you do not want.

Both methods are quick jobs, if you know how to do it.
Posted By: Grant

Re: 'Removing' the oldest line/row from a .CSV file - 09/13/17 10:49

Sorry for my late response Andrew. My csv file contains hundreds of calculated values, so I prefer not to calculate them every minute for multiple symbols. I believe that a DLL call is the way to go, since I'm not familiar enough with lite-c to rewrite this code from scratch. I've made/called DLLs, written in Pascal in the past, so I'm currently figuring out how to make/call this one in C or C++. Thanks for your suggestions.

Grant
© 2024 lite-C Forums