Hi
when I was looking on some thread, a question :?: comes to my mind, when you have a file like the one Log.csv from Aldin post, do you need to put it in a dynamic array to get the max and the min and the date..etc.
or you can only open the file and built some function to get the max the min ..etc.

thank you

Dani AI

Generated

Both and are right: it isn’t necessary to load the whole CSV into a dynamic array just to get min and max. For a simple min/max (and the date/time where they occur) a single pass over the file is the simplest and most memory-efficient approach. Keeping only the current min/max values and the associated timestamp uses constant memory and is much faster on large logs. Dynamic storage becomes useful only when later operations require revisiting rows (sorting, median, many-column aggregation, or random access).

A compact, practical C pattern is below (assumes first column is a date string and second column a numeric value; robust CSV parsing is a separate concern):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

int main(void) {
    FILE *f = fopen("Log.csv","r");
    if (!f) { perror("Log.csv"); return 1; }
    char line[1024], date[128], minDate[128] = "", maxDate[128] = "";
    double minVal = DBL_MAX, maxVal = -DBL_MAX;
    int lineno = 0;
    while (fgets(line, sizeof line, f)) {
        lineno++;
        if (lineno == 1 && strstr(line, "Date")) continue; // simple header skip
        line[strcspn(line, "\r\n")] = '\0';
        char *tok = strtok(line, ",");
        if (!tok) continue;
        strncpy(date, tok, sizeof date); date[sizeof date-1] = '\0';
        tok = strtok(NULL, ",");
        if (!tok) continue;
        char *end; double v = strtod(tok, &end);
        if (end == tok) continue;
        if (v < minVal) { minVal = v; strncpy(minDate, date, sizeof minDate); }
        if (v > maxVal) { maxVal = v; strncpy(maxDate, date, sizeof maxDate); }
    }
    fclose(f);
    if (minDate[0]) printf("Min: %g at %s\nMax: %g at %s\n", minVal, minDate, maxVal, maxDate);
    return 0;
}

Cautions and tips: strtok and this snippet won’t handle quoted fields or commas inside fields — use a proper CSV parser or implement quote-aware parsing for production code. Use getline() (or a dynamic buffer) if lines may exceed the fixed buffer. For per-column mins on many columns, maintain arrays of min/max values. For marking the thread solved, the typical forum options are a “Mark Solved” control or editing the subject to add “[SOLVED]” if the site UI lacks a direct button.

Recommended Answers

All 4 Replies

If I understand that thread correctly (I'm not sure I do, but I'll do my best), what you want to find is the maximum and minimum values for each row in the "table". What I would personally do would be to read in the file a line at a time, and store the first value as both the maximum and the minimum. You'd then loop through and compare the current value to the recognised maximum and minimum values, and make changes as necessary until you have the results you want. You'd then store the info in a std::vector of structures or something.

Sorry for not making my self clear enough, if you see the following thread
http://www.daniweb.com/techtalkforums/thread36795-1.html
so if you check this thread you will find that they put the log.csv file in a dynamic array, before finding the min and the max ( is this necesary :?: ) or they can only open the Log.csv file and then make some function to find the min and the max.

thanks in advance

It is not necessary to actually store the data in a vector or some other array. Just keep track of the min and max values as the data is read from the file.

can I know how you mark something solve.

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.