Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 29, 2006 Inputting comma-delimited data | ||||
---|---|---|---|---|
| ||||
I am writing a command-line C program to perform cubic-pline interpolation on large data files. Each line of the ASCII files will have: x-value,y-value. The values will be converted to long doubles via _atold and then put into separate x-arrays and y-arrays. I can probably eventually write code to properly input the data, but I am hoping someone already has a solid routine for doing it. Thanks in advance for your help. rvw |
January 29, 2006 Re: Inputting comma-delimited data | ||||
---|---|---|---|---|
| ||||
Posted in reply to rvw | rvw wrote: > I am writing a command-line C program to perform cubic-pline interpolation on > large data files. Each line of the ASCII files will have: x-value,y-value. The > values will be converted to long doubles via _atold and then put into separate > x-arrays and y-arrays. > > I can probably eventually write code to properly input the data, but I am hoping > someone already has a solid routine for doing it. > > Thanks in advance for your help. > > rvw > > fscanf? -- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org |
January 29, 2006 Re: Inputting comma-delimited data | ||||
---|---|---|---|---|
| ||||
Posted in reply to rvw | rvw wrote: > I am writing a command-line C program to perform cubic-pline interpolation on > large data files. Each line of the ASCII files will have: x-value,y-value. The > values will be converted to long doubles via _atold and then put into separate > x-arrays and y-arrays. > > I can probably eventually write code to properly input the data, but I am hoping > someone already has a solid routine for doing it. You could use: #include <stdio.h> #include <stdlib.h> #include <string.h> double *X, *Y; int main() { FILE *f = fopen("test.dat", "rt"); char Line[256]; unsigned int AllocSize = 0, Size = 0, n; if(!f) { fprintf(stderr, "Failed to open test.dat"); return EXIT_FAILURE; } while(fgets(Line, sizeof(Line), f)) { char *s = strtok(Line, ", "); if(s) { double x = strtod(s, 0); if((s = strtok(0, ",")) != 0) { double y = strtod(s, 0); if(Size >= AllocSize) { AllocSize += 128; X = realloc(X, AllocSize*sizeof(double)); Y = realloc(Y, AllocSize*sizeof(double)); } X[Size] = x; Y[Size] = y; Size++; } else { fprintf(stderr, "Wrong line in input"); } } else { fprintf(stderr, "Wrong line in input"); } } for(n = 0; n < Size; n++) { printf("%3u: %f %f\n", n, X[n], Y[n]); } free(X); free(Y); return EXIT_SUCCESS; } I C++ is an option it would be a lot easyer to do it with ifstream and a vector -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel |
January 29, 2006 Re: Inputting comma-delimited data | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bertel Brander | Bertel Brander wrote: > I C++ is an option it would be a lot easyer to do it with > ifstream and a vector Just for your reference, this is the C++ version of the same program: #include <vector> #include <iostream> #include <iomanip> #include <fstream> std::vector<double> X; std::vector<double> Y; int main() { std::ifstream File("test.dat"); if(!File) { std::cerr << "Failed to open test.dat" << std::endl; return 0; } double x, y; while(File >> x) { File.ignore(1024, ','); File >> y; X.push_back(x); Y.push_back(y); } for(std::vector<double>::size_type idx = 0; idx < X.size(); idx ++) std::cout << std::setw(3) << idx << ": " << X[idx] << " " << Y[idx] << std::endl; } -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel |
January 30, 2006 Re: Inputting comma-delimited data | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bertel Brander | In article <drjj8o$vuk$1@digitaldaemon.com>, Bertel Brander says... > >rvw wrote: >> I am writing a command-line C program to perform cubic-pline interpolation on large data files. Each line of the ASCII files will have: x-value,y-value. The values will be converted to long doubles via _atold and then put into separate x-arrays and y-arrays. >> >> I can probably eventually write code to properly input the data, but I am hoping someone already has a solid routine for doing it. > >You could use: > >#include <stdio.h> >#include <stdlib.h> >#include <string.h> > >double *X, *Y; > >int main() >{ > FILE *f = fopen("test.dat", "rt"); > char Line[256]; > unsigned int AllocSize = 0, Size = 0, n; > > if(!f) > { > fprintf(stderr, "Failed to open test.dat"); > return EXIT_FAILURE; > } > while(fgets(Line, sizeof(Line), f)) > { > char *s = strtok(Line, ", "); > if(s) > { > double x = strtod(s, 0); > if((s = strtok(0, ",")) != 0) > { > double y = strtod(s, 0); > if(Size >= AllocSize) > { > AllocSize += 128; > X = realloc(X, AllocSize*sizeof(double)); > Y = realloc(Y, AllocSize*sizeof(double)); > } > X[Size] = x; > Y[Size] = y; > Size++; > } > else > { > fprintf(stderr, "Wrong line in input"); > } > } > else > { > fprintf(stderr, "Wrong line in input"); > } > } > > for(n = 0; n < Size; n++) > { > printf("%3u: %f %f\n", n, X[n], Y[n]); > } > free(X); > free(Y); > return EXIT_SUCCESS; >} > >I C++ is an option it would be a lot easyer to do it with ifstream and a vector > >-- >Absolutely not the best homepage on the net: >http://home20.inet.tele.dk/midgaard >But it's mine - Bertel Bertel, Much thanks! I would have had a lot of trouble with memory allocation. And I was going to implement the functionality of STRTOK() from scratch! I will post my cubic-spline program after I have completed it. Thanks again! rvw |
Copyright © 1999-2021 by the D Language Foundation