Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, AndrewAMD), 911 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
'Removing' the oldest line/row from a .CSV file #467923
09/07/17 23:25
09/07/17 23:25
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
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.
Re: 'Removing' the oldest line/row from a .CSV file [Re: Grant] #467924
09/07/17 23:48
09/07/17 23:48
Joined: Nov 2016
Posts: 69
USA
J
jrath Offline
Junior Member
jrath  Offline
Junior Member
J

Joined: Nov 2016
Posts: 69
USA
That header file you found looks like it is C but Zorro is Lite-C. So not compatible.

Re: 'Removing' the oldest line/row from a .CSV file [Re: jrath] #467930
09/08/17 11:18
09/08/17 11:18
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
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

Re: 'Removing' the oldest line/row from a .CSV file [Re: Grant] #467935
09/08/17 15:13
09/08/17 15:13
Joined: Feb 2017
Posts: 1,727
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,727
Chicago
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.

Re: 'Removing' the oldest line/row from a .CSV file [Re: AndrewAMD] #468015
09/13/17 10:49
09/13/17 10:49
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
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

Last edited by Grant; 09/13/17 10:50.

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1