|
|
Subject:
Simple program to add a value to a elevation set
Category: Computers > Programming Asked by: alakon2-ga List Price: $2.00 |
Posted:
27 Aug 2003 08:50 PDT
Expires: 10 Sep 2003 07:01 PDT Question ID: 249242 |
|
There is no answer at this time. |
|
Subject:
Re: Simple program to add a value to a elevation set
From: eadfrith-ga on 28 Aug 2003 11:11 PDT |
I think the best option given your need for portability is to stick to standard C. I'm very rusty at C, but I think the following code should do what you want. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { FILE *fpin, *fpout; float diff; if(argc != 4) { printf("Usage: elev_change <input_file> <output_file> <elev_diff> \n"); return 1; } if((fpin = fopen(argv[1], "r")) == NULL) { printf("Cannot open input file: %s \n", argv[1]); return 1; } if((fpout = fopen(argv[2], "w")) == NULL) { printf("Cannot create output file: %s \n", argv[2]); return 1; } diff = atof(argv[3]); while(1) { float lat, lon, elev; if(fscanf(fpin, "%f %f %f", &lat, &lon, &elev) < 3) { break; } fprintf(fpout, "%f %f %f\n", lat, lon, (elev+diff)); } fclose(fpin); fclose(fpout); return 0; } You'll have to compile it yourself on each intended platform. Usage is then: elev_change <input_file> <output_file> <elev_diff> where: elev_change - the name you give to the program <input_file> - the file containing the data <output_file> - the name of the file to hold the modified data <elev_diff> - the value by which to change elevations (eg. 23.2, -34.77 etc). Cheers, Eadfrith |
Subject:
Re: Simple program to add a value to a elevation set
From: eadfrith-ga on 28 Aug 2003 14:35 PDT |
Alkalon2, The tabs are easy. Setting the significant figures is easy if it's the same for all elevations. For example, to get tabbed output with 2 significant figures replace the line: fprintf(fpout, "%f %f %f\n", lat, lon, (elev+diff)); with this: fprintf(fpout, "\t%f \t%f \t%.2f\n", lat, lon, (elev+diff)); This will give you 2 places after the decimal point. Change the ".2" to whatever format you wish. Eg. to get 4 places you'd used ".4" Now, if the input has variable precision, eg. some elevations are to 2 places while others are to 5, and you wish to match this line by line, then it's a little more tricky. If this is the case then let me know and I'll have a go. Cheers, Eadfrith |
Subject:
Re: Simple program to add a value to a elevation set
From: yosarian-ga on 07 Sep 2003 09:57 PDT |
At the risk of opening a language war, I suggest the following: we have here a program which needs to be very portable, which does a lot of file I/O and a little arithmetic. In my book, this is a reasonable candidate for a PERL script. Here's a reference saying it may be fast enough: http://www.developer.com/lang/perl/article.php/1554501 Here is my version: #!/usr/local/bin/perl -w if($#ARGV != 2){ die("Usage: elev_change <input_file> <output_file> <elev_diff>\n"); } $infile = shift(@ARGV); open(INFILE,"$infile") || die "Cannot open $infile"; $outfile = shift(@ARGV); open(OUTFILE,">$outfile") || die "Cannot open $outfile"; $diff = shift(@ARGV); while(<INFILE>){ ($lat,$lon,$elev) = ($_ =~ /^(-?\d+.\d*)\s+(-?\d+.\d*)\s+(-?\d+.\d*)\s*$/); printf OUTFILE "\t %f \t%f \t%.2f\n",$lat,$lon,$elev+$diff; } close(INFILE); close(OUTFILE); In UNIX, write this into a text file named elev_change and use eadfrith-ga's command line (You may have to dig a little to find the place of the perl interpreter defined in the first line of the script. I also stole eadfrith's output formatting. The strange line in the middle is a regular expression to identify three floats - I guess this could be more readable). This will run anywhere with a PERL interpreter - I believe all the platforms you have mentioned and many more. Cheers, yosarian-ga |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |