Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
#include <string.h>
#define MAXC 1024    
#define MAXDT  16

int main (int argc, char **argv) {
    char datestr[MAXDT]; 
    unsigned dateval;
    char buf[MAXC];  
    
    FILE * fp = fopen("file.csv", "r");
    if (!fp) { 
        perror ("file open failed");
        return 1;
    }
    
    while (fgets (buf, MAXC, fp)) {   
        unsigned mo, day, yr;    
        char *p = strrchr (buf, ','), tmpstr[MAXDT] = "";
        unsigned tmpu = 0;      
        
        if (!p)      
          continue;
        
        if(sscanf(p + 1, "%u-%u-%4u", &day, &mo, &yr)!= 3)
            continue;
       
        sprintf(tmpstr, "%04u%02u%02u", yr, mo, day); 
        
        if(sscanf(tmpstr, "%u", &tmpu) != 1) 
            continue;
        
        if(tmpu > dateval){   
            dateval = tmpu;     
            strcpy(datestr, p + 1);
            datestr[strcspn (datestr, "\n")] = 0;  
        }
    }
    if (fp != stdin)   
        fclose (fp);
    
    printf ("last date in file: %s\n", datestr); 
}


What I have tried:

In this code, I'm getting the file data and printing the latest date from it. What if there are two files and I want to read both files at the same. Will I have to use fgets() to read and repeat all the code again or is there any short method that is to read both files and print their data at the same time?
Posted
Updated 21-Oct-20 1:22am
v2
Comments
KarstenK 21-Oct-20 13:15pm    
you must organize your code with functions and structs so you can read and access the data from both files. These operations are in the milliseconds time frame.

Quote:
How to read two files data at the same time?

Rather simple:
C++
// Open fist file
FILE * fp_first = fopen("file1.csv", "r");
// open second file
FILE * fp_second = fopen("file2.csv", "r");

// read first file
fgets (buf, MAXC, fp_first);
// read second file
fgets (buf, MAXC, fp_second);
 
Share this answer
 
It's not actually possible in C to read two files at the same time, at least not without a lot of faffing as it has no inbuilt support for multiple threading - though threading libraries are generally available for most operating systems they may or may not be compatible with your version of C (it's a very old language now)

The best you can do is read a line from one, a line from the other, then process both as if they were read simultaneously, and loop to process all lines in both files. Which will take some effort as you have to cope with one file "running out" before the other does.

I'm not saying it can't be done, just that you need to think long and hard about exactly what your file contains and how you need to process the two files before you start jumping onto code: it might be better to read and preprocess each file in advance of any "combined data" operations to get anything useful out of it. But that will depend on the data and what exactly you want to do with it, and we have no idea on that!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900