#include #include #include #include #include #include int main(int argc, char** argv) { int i; FILE* fp; FILE* pipep; int count = 0; char* command; int lines; float* data; int j; char* target; int fd; if ( argc < 2 ) { fprintf(stderr, "Usage %s files-to-check\n", argv[0]); return -1; } for ( i = 1; i < argc; i++ ) { fp = fopen(argv[i],"r"); if ( fp != 0 ) { /* figure out how many lines */ command = (char*)malloc(8+strlen(argv[i])); sprintf(command, "wc -l %s", argv[i]); pipep = popen(command, "r"); free(command); fscanf(pipep, "%d", &lines); pclose(pipep); data = (float*)malloc(lines*2*sizeof(float)); for ( j = 0, count = 0; ; count++ ) { if ( fscanf(fp, "%f,%f", &data[j], &data[j+1]) != EOF ) { j += 2; } else { break; } } assert(count == lines); fclose(fp); target = strstr(argv[i],".csv"); strcpy(target, ".dat"); fd = open(argv[i], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); count = write(fd, data, lines*2*sizeof(float)); assert(count == lines*2*sizeof(float)); close(fd); free(data); } else { fprintf(stderr, "%s: Can't open %s\n", argv[0], argv[i]); return -2; } fclose(fp); } return 0; }