Exporting to Excel

I'm trying to import table from a txt, isolate and change some of the data (ex: utc to gps time), and export that new data into excel, probably as a csv. The text file where I'm getting data has hundreds of lines, and looks like this:

#YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS PTDY TIDE
#yr mo dy hr mn degT m/s m/s m sec sec degT hPa degC degC degC nmi hPa ft
2013 07 25 12 00 260 10.8 12.4 0.5 4 MM MM MM MM 29.1 MM 6.5 MM MM
2013 07 25 11 00 270 10.8 11.8 0.5 4 MM MM MM MM 29.3 MM 6.5 MM

I've managed to take some of the data such as year, month, day, hour, etc and convert that to gps (see code below), as well as taking the waveheight data, which other than time is all I need. When I try to export the gps time and wave height to a csv file, it only displays data from the very last line of the text file. Any way I could fix that or do this more easily? Thanks for the help. And yes, I've read the tutorials and they don't help.

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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
	FILE *in_file;
	in_file = fopen("/users/christopher/desktop/spll1.txt","r");
	FILE *out_file;
	out_file = fopen("/users/christopher/desktop/testing.txt","w");
	char s[100];
	if (in_file == NULL || out_file == NULL)
    {
        printf("Error: File Not Found");
        exit(-1);
    }
	while (fgets(s,100,in_file)!=NULL)
    {
		int y;
		int M;
		int d;
		int h;
		int m;
		float b;
		float c;
		float f;
		float w;
		int l;
		int a;
		unsigned long t;
		fscanf(in_file, "%d %d %d %d %d %f %f %f %f", &y, &M, &d, &h, &m, &b, &c, &f, &w);
		//leap year stuff
		l=(y-1984)/4*86400;
		if (y%4==0 && y%100!=0)
        {
			a=86400;
        }
        else  {a=0;}
		//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;}
		//month
		switch (M)
        {
			case (1):        //jan, 31
                t =t+l-a; //(y-1984) is a simplification of (y-4-1980)...
                break;
			case (2):       //feb, 28/29
                t = t+2678400+l-a; //notice that the added time is cumulative from month to month
                break;
			case (3):       //mar, 31
                t = t+5097600+l; // we consider current leap years from here on out
                break;
			case (4):      //apr, 30
                t = t+7776000+l;
                break;
			case (5):      //may, 31
                t = t+10368000+l;
                break;
			case (6):      //jun, 30
                t = t+13046400+l;
                break;
			case (7):      //jul, 31
                t = t+15638400+l;
                break;
			case (8):      //aug, 31
                t = t+18316800+l;
                break;
			case (9):      //sep, 30
                t = t+20995200+l;
                break;
			case (10):      //oct, 31
                t = t+23587200+l;
                break;
			case (11):      //nov, 30
                t = t+26265600+l;
                break;
			case (12):      //dec, 31
                t = t+28857600+l;
                break;
        }
        
        
        
		t = t+d*86400;          //days
        
		t = t+h*3600;           //hours
        
		t = t+m*60;             //minutes
        
        
        
        //leap seconds  ====== There is no way to predict leap seconds, change t=t+_ as more leap seconds since 1980 occur ======
        
		t=t+16; 
        
        
        ofstream MyExcelFile;
        MyExcelFile.open("/users/christopher/desktop/Example.csv");
        
        MyExcelFile << "GPS Time, Wave Height" << endl;
        MyExcelFile << t << "," << w << endl;
        MyExcelFile.close();
        
		printf("GPS Time: %lu Wave Height: %.1f \n",t,w);
    }
	fclose(in_file);
	fclose(out_file);
	return 0;
}
Topic archived. No new replies allowed.