Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Critique my C code

Name: Anonymous 2013-03-29 1:08

Took me about 2 hours to write this. There are some areas I know of to improve I just wanted to get it done. I'm just asking because I want to know if I should continue programming or if I should start scrubbing toilets with Kodak-kun.

The program parses through data files and gives the mins and maxes for each field. It also auto detects the header and ignors it. It displays the mins and maxes of each field as well as the line number. In addition it will accept a directory instead of a single file and iterate through all files with a user defined extension.


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>

#define SINGLE_FILE 0
#define DIRECTORY 1

#define MAX_PATH 250
#define MAX_EXT 10
#define MAX_BUFF 250
#define DATA_MAX 50

void find_maxes(char *path);
void find_fields(char *path, int *fields, int *start_line);
int get_fields(char *s);
void split_data(double *data, char *s,  int fields);

int main(int argc, char **argv) {
  int type, file_count;
  char path[MAX_PATH], ext[MAX_EXT];
 
  if(argc > 2 && !strcmp(argv[1], "-s")) {
    type = SINGLE_FILE;
    if(strlen(argv[2]) > MAX_PATH) {
      fputs("Path length too long\n", stdout);
      exit(1);
    }
    strcpy(path, argv[2]);
  }
  else if(argc > 3 && !strcmp(argv[1], "-d")) {
    type = DIRECTORY;
    if(strlen(argv[2]) > MAX_PATH || strlen(argv[3]) > MAX_EXT) {
      fputs("Path or extension length too long\n", stdout);
      exit(1);
    }
    strcpy(path, argv[2]);
    strcpy(ext, argv[3]);
  }
  else {
    fprintf(stdout, "Usage: '%s -s file_path'\n"
    "or\n"
    "'%s -d directory_path extension'\n", *argv, *argv);
    exit(2);
  }
 
  if(type == SINGLE_FILE) {
    find_maxes(path);
  }
  else {
    struct dirent *dp;
    DIR *dfd = opendir(path);
    char fn[MAX_PATH], full_path[MAX_PATH];
    if(!dfd) {
      fprintf(stdout, "Can't open directory '%s'\n", path);
      exit(3);
    }
   
    while(dp = readdir(dfd)) {
      strcpy(fn, dp->d_name);
      if(!strncmp(fn+strlen(fn)-strlen(ext), ext, strlen(ext))) {
        fprintf(stdout, "In %s\n", fn);
        sprintf(full_path, "%s%s", path, fn);
        find_maxes(fn);
      }
    }
  }
  return(EXIT_SUCCESS);
}

void find_maxes(char *path) {
  int fields, start_line, line_no = 0, i;
  char buff[MAX_BUFF];
 
  find_fields(path, &fields, &start_line);
 
  FILE *f = fopen(path, "r");
 
  if(!f) {
    fprintf(stdout, "Error opening %s\n", path);
    perror("fopen");
  }
 
  double *data, *maxes, *mins;
  int *line_min, *line_max;
 
  data = (double *) malloc(sizeof(double) * fields);
  maxes = (double *) malloc(sizeof(double) * fields);
  mins = (double *) malloc(sizeof(double) * fields);
  line_min = (int *) malloc(sizeof(int) * fields);
  line_max = (int *) malloc(sizeof(int) * fields);
 
  for(i = 0; i < fields; i++) {
    maxes[i] = -1e100;
    mins[i] = +1e100;
    line_min[i] = 0;
    line_max[i] = 0;
  }
 
  while(fgets(buff, MAX_BUFF, f) && ++line_no < start_line)
    ;
   
  while(fgets(buff, MAX_BUFF, f)) {
    line_no++;
    if(get_fields(buff) != fields)
      continue;
    else
      split_data(data, buff, fields);
   
    for(i = 0; i < fields; i++) {
      if(data[i] > maxes[i]) {
        maxes[i] = data[i];
        line_max[i] = line_no;
      }
      if(data[i] < mins[i]) {
        mins[i] = data[i];
        line_min[i] = line_no;
      }
    }
  }
 
 
  for(i = 0; i < fields; i++)
    printf("Field %d: Max = %lf @ line %d\tMin = %lf @ line %d\n", i, maxes[i], line_max[i], mins[i], line_min[i]);
 
  free(data);
  free(maxes);
  free(mins);
  free(line_min);
  free(line_max);
  fclose(f);
}

void find_fields(char *path, int *fields, int *start_line) {
  FILE *f = fopen(path, "r");
  if(!f) {
    fprintf(stdout, "Error opening %s\n", path);
    perror("fopen");
  }
 
  int line_no = 0, fields_current, fields_last, n = 0;
  char buff[MAX_BUFF];
 
  while(fgets(buff, MAX_BUFF, f) && line_no++ < 1000) {
    fields_current = get_fields(buff);
    if(fields_current == fields_last) {
      if(n < 100) {
        *fields = fields_current;
        *start_line = line_no;
        return;
      }
      else {
        n++;
      }
    }
    else {
      n = 0;
    }
    fields_last = fields_current;
  }
 
  *fields = -1;
  *start_line = -1;
  fclose(f);
}

int get_fields(char *s) {
  int fields = 0;
  while(*s) {
    while((*s == ' ' || *s == '\t') && *s)
      *s++;
   
    while(!(*s == ' ' || *s == '\t') && *s)
      *s++;
    fields++;
  }
  return(fields);
}

void split_data(double *data, char *s,  int fields) {
  char buff[DATA_MAX];
  int j = 0, i;

  while(*s) {
    while((*s == ' ' || *s == '\t') && *s)
      *s++;

    i = 0;
    while(!(*s == ' ' || *s == '\t') && *s)
      buff[i++] = *s++;
    buff[i] = 0;

    data[j++] = atof(buff);

    if(j == fields)
      return;
    *s++;
  }
}

Name: Anonymous 2013-03-29 1:11

its bad

Name: Anonymous 2013-03-29 2:02

Do you enjoy programming?

if yes goto a)
else goto EOA)

a)
do you feel like you were born to do it?

if yes goto b) else goto EOA)

b)

are you willing to commit your time to learn, and recognize that you are a human being and therefore not going to be able to become good at anything without hard work and effort? Thisl includes scrubbing toilets.

if yes, goto c) else goto EOA)

c)

The fact that you took the time to 
write this program out of sheer interest means you
obviously enjoy the act of programming. You're a problem solver. Regardless of the code, recognizes 
its faults and be willing to improve their processes over time.

Getting it done is the first step, improving it is the second; while keeping it in a working condition as much as possible.

You care about the process a lot, but recognize the importance of the solution first. This is important.

Goto EOA)


EOA)

if you don't like programming that much, do something else. Recognize that regardless of where your passion lies, the most meaningful and fulfilling for you is where you will truly grow. Caring about others' perceptions of your code to the extent that you're willing to go off and "scrub toilets" isn't productive. You in your heart know what you want to do, it doesn't matter what someone else tells you in terms of whether or not you're "good enough" to do something. As long as you recognize your faults and are willing to improve them, you will succeed. Apply this mindset to everything else in life, and you will flourish as well. Good luck, namaste.

P.S., two year programmer here, with lots learned over time, but far more to continue to learn and grasp. Never give up on something you know is worth it.

Name: Anonymous 2013-03-29 3:32

Name: Anonymous 2013-03-29 3:39

>>1
You know awk and Perl already exist, right?

Name: Anonymous 2013-03-29 6:55

Stephen hawking is completely retarded: he's just smashing keys all "derp derp durr" and TPTB pre-program that fucking voice to say whatever they want him to say and the little hawking tards will believe it. Ever notice how his viewpoints are directly in line with TPTBs agenda? The guy is a vegetable.

Name: Anonymous 2013-03-29 7:40

why not ruby or perl or python

Name: Anonymous 2013-03-29 10:20

>>5,7
How could you idiots miss the point so hard

Name: Anonymous 2013-03-29 18:01

>>7
SLOW AS FUCK
What if I need to use it to process terabytes of data?

Name: Anonymous 2013-03-29 18:07

>>9
Then you're going to be blocking for I/O most of the time, and the speed of your language will hardly matter.

Name: Anonymous 2013-03-29 22:16

>>10
I'd love to hear your explanation of why today's filesystems don't do whole-disk compression by default.

Name: Anonymous 2013-03-30 6:28

>>11
People who aren't criminals prefer speed over over bloat.

Name: Anonymous 2013-03-30 7:59

>>11
His statement still holds (disk I/O is much slower), but even if it didn't, the performance of the implementation would hardly matter, since the file system operations aren't implemented by the language (so to speak).

Name: Anonymous 2013-03-30 12:53

>>13
This is ridiculous. Look through the code. Not all of the time spent is on the file input. A lot of it is analyzing the string, converting the string, then analyzing the converted numbers.

Doing this in a scripting language would be dramatically slower.

Name: Anonymous 2013-03-30 15:45

Just tested it out with the following source code

C: http://pastebin.com/ucBTJtbX
Perl: http://pastebin.com/uLG0wexe

The Perl script does less and when given a 168 MB file, the C program does it in 15 seconds while the Perl script takes 64.

Name: Anonymous 2013-03-30 17:24

>>15
Huh, I guess I was wrong.

Name: Anonymous 2013-03-30 22:37

>>15
That's pretty fucking fast for an interpreter.

Name: Anonymous 2013-03-31 0:44

>>17
The point was that different languages handle io differently.

Name: Anonymous 2013-03-31 1:48

>>18
I don't think that was the point.

Name: Anonymous 2013-03-31 2:05

>>19
Point in case.

Name: Anonymous 2013-03-31 2:05

>>20
Point in case.
Case in point.

Name: Anonymous 2013-03-31 2:06

>>21
Case in point.
Coint in pase.

Name: Anonymous 2013-03-31 2:06

>>22
Haskell
Cock in mouth.

Name: Anonymous 2013-03-31 2:25

>>23
Lisp
Cock in anus.

Name: KodakGalleryProgrammer 2013-03-31 2:49

What the hell. I haven't been on this board for a really really long time and yet you bitches are still saying my name!

Name: Anonymous 2013-03-31 2:59

>>25
Probability of authenticity....99.9%.

Hello kodak-san.

Name: sexwithjews !l5Ju6Ly8I2 2013-03-31 16:48

Name: KodakGalleryProgrammer !IDhmIZA.fg 2013-03-31 16:49

>>26
Hey. I claim to be the same person since like my trip code matches or what not.

Name: Anonymous 2013-03-31 16:50

>>28
Fuck, I either forgot what my trip code is or big daddy moot changed the fucking trip code thingy again.

Name: Anonymous 2013-03-31 17:13

>>29
world4ch's regular tripcode algorithm has never changed.

Name: Anonymous 2013-03-31 18:25

>>29
Probability of authenticity...12.1%

Hello one-who-misses-kodak-san-san.

Don't change these.
Name: Email:
Entire Thread Thread List