Thread: Filestat

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Filestat

    Haveing problem with this code, the program computes basic statistic on a single file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <memory.h>
    #include <math.h>
    
    typedef struct_StatDat {
    
     int size;
     double avg, var, skew, kurt, avg_dev, prev, self;
     int diff_vals;
    }
    
    int FileStats(char *fn, StatDat *stats );
    
    void main(int argc, char* argv[]) {
      int test;
      char fn[128];
      StatDat stats;
    
      strcopy(fn, argv[1]);
      test = FileStats(fn, &stats);
      if(test == 0){
        printf(" %.3f %.3f %.3f %.3f %.3f %.4f %.4f %d\n", stats.avg, stats.var, stats.skew, 
           stats.kurt, stats.avg_dev, stats.prev, stats.self, stats.diff_vals);
      }
      else {
        printf("State returned error code %d\n", test);
      }
    }
    
    int FileStats(char *fn, StatDat *stats){
      int i, got, vals[256];
      int ParCnt, parity[2], SelfCnt;
      FILE *Fin;
      double d_temp, d_temp2, correct;
      unsigned char buffer[1024];
    
      Fin = fopen(fn, "rb");
      if(!Fin){
        printf("Unable to open file %s\n", fn);
        return(1);
      }
    
      d_temp = 0.0;
      stats->avg_dev = 0.0;
      stats->var = 0.0;
      stats->skew = 0.0;
      stats->kurt = 0.0;
      stats->diff_vals = 0.0;
      memset(vals, 0, 256*sizeof(int));
      memset(parity, 0, 2*sizeofint(int));
      ParCnt = SelfCnt = 0;
    
      got = fread(buffer, 1, 1024, Fin);
      while(got){
        for(i=0; i<got; i++){
          d_temp += (double) buffer[i];
          vals[buffer[i]] ++;
          parity[0] = buffer[i] % 2;
          if(parity[0] == parity[1]) { ParCnt++; }
          if(parity[0] == (buffer[i]/128)) { SelfCnt++; }
          parity[1] = parity[0];
        }
        got = fread(buffer, 1, 1024, Fin);
      }
    
      stats->size = ftell(Fin);
      stats->avg = d_temp*1.0/stats->size;
      stats->prev = ParCnt*1.0/stats->size;
      stats->self = SelfCnt*1.0/stats->size;
    
      fseek(Fin, 0, 0);
      got = fread(buffer, 1, 1024, Fin);
      correct = 0.0;
      while(got){
        for(i=0; i<got; i++){
          d_temp = buffer[i] - stats->avg;
          stats->avg_dev += fabs(d_temp);
          correct += d_temp;
          d_temp2 = d_temp * d_temp;
          stats->var += d_temp2;
          stats->skew += d_temp2 * d_temp;
          stats->kurt += d_temp2 * d_temp2;
        }
        got = fread(buffer, 1, 1024, Fin);
      }
      stats->avg_dev /= stats->size;
      stats->var = (stats->var - correct*correct/stats->size)/(stats->size-1);
      if(stats->var > 0.0) {
        stats->skew /= stats->size *
          stats->var * sqrt(stats->var);
        stats->kurt /= stats->sie*stats->var*stats->var;
        stats->kurt -= 3.0;
      }
      else {
        stats->skew = 0.0;
        stats->kurt = 0.0;
      }
    
      for(i=0, i<256; i++){
        if(vals[i]){
          stats->diff_vals++;
        }
      }
    
      fclose(Fin);
      return(0);
    }
    I get the following errors compileing useing gcc through Cygwin:

    $ gcc filestat.c
    filestat.c:7: error: syntax error before '{' token
    filestat.c:12: error: parse error before '}' token
    filestat.c:14: error: parse error before "StatDat"
    filestat.c: In function `main':
    filestat.c:19: error: `StatDat' undeclared (first use in this function)
    filestat.c:19: error: (Each undeclared identifier is reported only once
    filestat.c:19: error: for each function it appears in.)
    filestat.c:19: error: parse error before "stats"
    filestat.c:22: error: `stats' undeclared (first use in this function)
    filestat.c:16: warning: return type of `main' is not `int'
    filestat.c: At top level:
    filestat.c:33: error: parse error before "StatDat"
    filestat.c: In function `FileStats':
    filestat.c:40: error: `fn' undeclared (first use in this function)
    filestat.c:47: error: `stats' undeclared (first use in this function)
    filestat.c:53: error: parse error before "int"
    filestat.c:102: error: parse error before ')' token
    filestat.c: At top level:
    filestat.c:108: warning: parameter names (without types) in function declaration

    filestat.c:108: warning: data definition has no type or storage class
    filestat.c:109: error: parse error before "return"
    Last edited by Rhidian; 02-03-2005 at 05:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your typedef is missing a ;
    and your main is all wrong
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Here's the fixed code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <memory.h>
    #include <math.h>
    
    typedef struct tagStatDat {
     int size;
     double avg, var, skew, kurt, avg_dev, prev, self;
    
     int diff_vals;
    } StatDat;
    
    int FileStats(char *fn, StatDat *stats );
    
    int main(int argc, char* argv[]) {
      int test;
      char fn[128];
    
      StatDat stats;
    
      strcpy(fn, argv[1]);
      test = FileStats(fn, &stats);
      if(test == 0){
        printf(" %.3f %.3f %.3f %.3f %.3f %.4f %.4f %d\n", stats.avg, stats.var, stats.skew, 
    
           stats.kurt, stats.avg_dev, stats.prev, stats.self, stats.diff_vals);
      }
      else {
        printf("State returned error code %d\n", test);
      }
    
    }
    
    int FileStats(char *fn, StatDat *stats){
      int i, got, vals[256];
      int ParCnt, parity[2], SelfCnt;
      FILE *Fin;
      double d_temp, d_temp2, correct;
    
      unsigned char buffer[1024];
    
      Fin = fopen(fn, "rb");
      if(!Fin){
        printf("Unable to open file %s\n", fn);
    
        return(1);
      }
    
      d_temp = 0.0;
      stats->avg_dev = 0.0;
      stats->var = 0.0;
    
      stats->skew = 0.0;
      stats->kurt = 0.0;
      stats->diff_vals = 0.0;
      memset(vals, 0, 256*sizeof(int));
      memset(parity, 0, 2*sizeof(int));
    
      ParCnt = SelfCnt = 0;
    
      got = fread(buffer, 1, 1024, Fin);
      while(got){
        for(i=0; i<got; i++){
          d_temp += (double) buffer[i];
    
          vals[buffer[i]] ++;
          parity[0] = buffer[i] % 2;
          if(parity[0] == parity[1]) { ParCnt++; }
          if(parity[0] == (buffer[i]/128)) { SelfCnt++; }
          parity[1] = parity[0];
        }
    
        got = fread(buffer, 1, 1024, Fin);
      }
    
      stats->size = ftell(Fin);
      stats->avg = d_temp*1.0/stats->size;
      stats->prev = ParCnt*1.0/stats->size;
    
      stats->self = SelfCnt*1.0/stats->size;
    
      fseek(Fin, 0, 0);
      got = fread(buffer, 1, 1024, Fin);
      correct = 0.0;
      while(got){
    
        for(i=0; i<got; i++){
          d_temp = buffer[i] - stats->avg;
          stats->avg_dev += fabs(d_temp);
          correct += d_temp;
          d_temp2 = d_temp * d_temp;
    
          stats->var += d_temp2;
          stats->skew += d_temp2 * d_temp;
          stats->kurt += d_temp2 * d_temp2;
        }
        got = fread(buffer, 1, 1024, Fin);
    
      }
      stats->avg_dev /= stats->size;
      stats->var = (stats->var - correct*correct/stats->size)/(stats->size-1);
      if(stats->var > 0.0) {
    
        stats->skew /= stats->size *
          stats->var * sqrt(stats->var);
        stats->kurt /= stats->size*stats->var*stats->var;
    
        stats->kurt -= 3.0;
      }
      else {
        stats->skew = 0.0;
        stats->kurt = 0.0;
    
      }
    
      for(i=0; i<256; i++){
        if(vals[i]){
          stats->diff_vals++;
        }
    
      }
    
      fclose(Fin);
      return(0);
    }


    Your errors:
    Code:
    typedef struct_StatDat {
    
     int size;
     double avg, var, skew, kurt, avg_dev, prev, self;
     int diff_vals;
    }
    First of all, a struct should be ended with a semi-colon. When typedef'ing a struct, it looks like this:

    Code:
    typedef struct optionalStructName {
    // variables
    } shortName;
    Code:
    void main(int argc, char* argv[]) {
    Void main?!

    Code:
      strcopy(fn, argv[1]);
    it's strcpy, not strcopy

    Code:
      memset(parity, 0, 2*sizeofint(int));
    sizeofint isn't a function, it's simply sizeof(int)

    Code:
        stats->kurt /= stats->sie*stats->var*stats->var;
    Guess you mistyped stats->sie, should be stats->size

    Code:
      for(i=0, i<256; i++){
    i=0, i<256 ? Should be i=0; i<256



    I think that was all... It really isn't hard to fix, but I think you're missing some basic C knowledge (eg: typedef'ing a struct)

Popular pages Recent additions subscribe to a feed