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

Last edited by Grant; 09/07/17 23:26.