Advertisement

01.24.2008 at 07:43AM PST, ID: 23107949
[x]
Attachment Details

Need guide for porting C code to Perl

Asked by Jerryleo in Perl Programming Language

Tags: Perl

I'm a newbie for Perl programming. I'd like to port a  C sample code to Perl script.  It's used to converte a plain text file to a  binary data file using specified data structure.

struct reportheader {
  char  id[8];    /* Station ID */
  float lat;      /* Latitude of Station */
  float lon;      /* Longitude of Station */
  float t;        /* Time in grid-relative units */
  int   nlev;        /* Number of data groups following the header */
  int   flag;     /* Level independent var set flag */
};

I failed to get the proper output from the perl code. Could anyone give me some comments for the right way.  

Thanks a lot!


Start Free Trial
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:
***********C code need to be ported**************
 
#include <stdio.h>
/* Structure that describes a report header in a stn file */ 
struct rpthdr {
  char  id[8];    /* Station ID */
  float lat;      /* Latitude of Station */
  float lon;      /* Longitude of Station */ 
  float t;        /* Time in grid-relative units */ 
  int   nlev;	  /* Number of levels following */ 
  int   flag;     /* Level independent var set flag */ 
} hdr;
 
main () 
{
  FILE  *ifile, *ofile; 
  char  rec[80]; 
  int   flag,year,month,yrsav,mnsav,i; 
  float val;
 
/* Open files */ 
  ifile = fopen ("rain.ch","r"); 
  ofile = fopen ("rain.dat","wb"); 
  if (ifile==NULL || ofile==NULL) { 
    printf("Error opening files\n"); 
    return; 
  }
 
/* Read, write loop */
  flag = 1; 
  while (fgets(rec,79,ifile)!=NULL) { 
    /* Format conversion */ 
    sscanf (rec,"%i %i",&year,&month); 
    sscanf (rec+20," %g %g %g",&hdr.lat,&hdr.lon,&val); 
    for (i=0; i<8; i++) hdr.id[i] = rec[i+11]; 
    /* Time group terminator if needed */ 
    if (flag) { 
      yrsav = year; 
      mnsav = month;
      flag = 0; 
    } 
    if (yrsav!=year || mnsav!=month) { 
      hdr.nlev = 0; 
      fwrite(&hdr,sizeof(struct rpthdr), 1, ofile); 
    } 
    yrsav = year; 
    mnsav = month;
    /* Write this report */ 
    hdr.nlev = 1;
    hdr.flag = 1; 
    hdr.t = 0.0;
    fwrite (&hdr,sizeof(struct rpthdr), 1, ofile);
    fwrite (&val,sizeof(float), 1, ofile); 
  } 
  hdr.nlev = 0; 
  fwrite (&hdr,sizeof(struct rpthdr), 1, ofile); 
}
 
 
****************My Perl Code*********************
 
#!/usr/bin/perl
 
die "\nUsage: $0 infilename outfilename\n" if $#ARGV < 1;
 
($infilename, $outfilename) = @ARGV;
 
open(INF, "<$infilename")  or die "\nCan't open $infilename for reading: $!\n";
 
open(OUTF, ">$outfilename") or die "\nCan't open $outfilename for writing: $!\n";
 
binmode OUTF;
 
$iFlag=1;
 
$n=1;
 
while (<INF>) {
 
#  s/#.*//;            # ignore comments by erasing them
#  next if /^(\s)*$/;  # skip blank lines
 
   my($LINE)=$_;
 
   chomp($LINE);
 
   ($STID,$LAT,$LON,$Yr,$Doy,$Hr,$Mn,$Sec,$TZen,$WZen,$SigZen,$PW,$SigPW,$Press,$Temp,$ZHD)= split(/\s+/,$LINE);
#   print $PW,"\n";
 
 if ( $iFlag == 1) {
    $LastYr=$Yr;
    $LastDoy=$Doy;
    $LastHr=$Hr;
    $LastMn=$Mn;
    $LastSec=$Sec;
    $iFlag=0 ;
  }
 
  if ( $LastYr != $Yr || $LastDoy != $Doy || $LastHr != $Hr || $LastMn != $Mn || $LastSec != $Sec ) {
    $nLev=0;
    print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
    print $n,"\n";
  }
 
  $nFlag=1;
  $nLev=1;
  $TIM=0.0;
 
  $LastYr=$Yr;
  $LastDoy=$Doy;
  $LastHr=$Hr;
  $LastMn=$SMn;
  $LastSec=$Sec;
 
  print $STID,$LAT,$LON,$TIM,$nLev,$nFlag;
  print $PW;
  print "\n";
 
#  print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
  print OUTF pack("A8dddii",$STID,$LAT,$LON,$TIM,$nLev,$nFlag);
  print OUTF pack("d",$PW);
 
} 
 
$nLev=0;
 
print $STID,$LAT,$LON,$TIM,$nLev,$nFlag;
print $PW;
print "\n";
 
#print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
print OUTF pack("A8dddii",$STID,$LAT,$LON,$TIM,$nLev,$nFlag);
 
close(OUTF) or die "Can't close $destfile: $!\n";
close(INF)  or die "Can't close $srcfile: $!\n";
[+][-]01.24.2008 at 09:18AM PST, ID: 20734983

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 05:21PM PST, ID: 20739276

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 08:10PM PST, ID: 20740026

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 09:15PM PST, ID: 20740267

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 11:09PM PST, ID: 20740641

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 11:36PM PST, ID: 20740762

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.25.2008 at 04:57AM PST, ID: 20742019

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.25.2008 at 07:08AM PST, ID: 20743068

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: Perl
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.25.2008 at 07:22AM PST, ID: 20743179

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628