Export and Sort in Excel

Ok, this is a bit complicated. Essentially what I'm doing is creating a program that can pull data from a website, modify it, and export it to excel (as a csv). To pull the data from online, I used perl, and run that with system. That creates a txt which I can access through C++ and modify. I am able to export it to excel, but when I append new data the times are all out of order. Is there any way I can export to excel AND sort numerically by GPS time?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h> // These are the necessary libraries to perform what we need to do.
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std; // Allows for functions of multiple libraries to be used

int main () // The main function that has all the operations we desired

{
    system("cd /users/christopher/desktop/WaveArchive/ && perl data.pl"); // Launches Perl script to extract the txt file from a website and put it in a desired directory
    FILE *in_file; // This declares the input file
    in_file = fopen("/users/christopher/desktop/WaveArchive/spll1.txt","r"); // This opens the input file and says it will be read.  Change path to file as needed
    char s[100]; // If the file cannot be found, report the error to the console
    if (in_file == NULL)
        
    {
        printf("Error: File Not Found");
        exit(-1);
    }
    
    while (fgets(s,100,in_file)!=NULL)  // This reads the document line by line and cancels when at the end        
    {
        int y; // All of these variables are vital for time data isolation and subsequent calculation
        int M;
        int d;
        int h;
        int m;
        float b;
        float c;
        float f;
        float w;
        int l;
        int a;
        unsigned long t; // This is the final product variable, t for time.  It is a large (long) number that can only be positive (unsigned)
        
        fscanf(in_file, "%d %d %d %d %d %f %f %f %f", &y, &M, &d, &h, &m, &b, &c, &f, &w); // Sifts through the input file and assigns a variable to every number and string
        
        
        //**time calculations**  dealing with leap years
        
        l=(y-1984)/4*86400;
        if (y%4==0 && y%100!=0)
            
        {
            a=86400;
        }
        
        else  {a=0;}
        
        // Add seconds because of the year
        
        if (y>=1981)
        {
            t= (y-1981)*31536000+31190400; // The amount of seconds from the GPS epoch to the start of this year
        }
        
        else  {t=0;}
        
        // Add seconds because of the month
        
        switch (M)
        {
            case (1):        // If it is January add X seconds:
                t =t+l-a;    // The  "-a" is to deal with leap days that haven't yet happened in the year
                break;
                
            case (2):       // If it is February add X seconds:
                t = t+2678400+l-a; // Notice how the added time is cumulative from month to month
                break;
                
            case (3):       // If it is March add X seconds:
                t = t+5097600+l; // We consider current leap years from here on out, no more "-a"
                break;
                
            case (4):      // If it is April add X seconds:
                t = t+7776000+l;
                break;
                
            case (5):      // If it is May add X seconds:
                t = t+10368000+l;
                break;
                
            case (6):      // If it is June add X seconds:
                t = t+13046400+l;
                break;
                
            case (7):      // If it is July add X seconds:
                t = t+15638400+l;
                break;
                
            case (8):      // If it is August add X seconds:
                t = t+18316800+l;
                break;
                
            case (9):      // If it is September add X seconds:
                t = t+20995200+l;
                break;
                
            case (10):      // If it is October add X seconds:
                t = t+23587200+l;
                break;
                
            case (11):      // If it is November add X seconds:
                t = t+26265600+l;
                break;
                
            case (12):      // If it is December add X seconds:
                t = t+28857600+l;
                break;
        }
        
        t = t+d*86400;          // Seconds added according to days
        t = t+h*3600;           // Seconds added according to hours
        t = t+m*60;             // Seconds added according to minutes
        
        /*Leap seconds
         
         ====== There is no way to predict leap seconds, change t=t+X as more leap seconds since 1980 occur.  There were 16 leap seconds as of 7/23/2013. ======*/
        
        t=t+16;
        
        // The GPS time is now accurate and stored in the variable t
        
        if (t>16)  // This deals with a bug encountered when the console reads the first line
            
        {
            ofstream MyExcelFile2;  // Outputting the manipulated data to a csv file
            MyExcelFile2.open("/users/christopher/desktop/WaveArchive/WaveArchive.csv", std::ios_base::app);  //Opening a csv file to be appended to
            MyExcelFile2 << t << "," <<  M << "/" << d << "/" << y << "," << h << ":" << m << "," << w << "," << endl; // Isolating time and wave height to be printed to the file.. and then printing those numbers to it
            MyExcelFile2.close(); // Closing the csv file, the csv file now has the desired data
        }
    }
    
    fclose(in_file); // Closing the input file, the final output WaveArchive.csv has been placed into the directory and the program is done.
    
    return 0;
}
Topic archived. No new replies allowed.