Advertisement
whitequark

Untitled

Apr 7th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char** argv) {
  4.   FILE *input, *output;
  5.   int format, N, M, i, j;
  6.  
  7.   if(argc != 6 || (strcmp(argv[3], "d") && strcmp(argv[3], "f"))) {
  8.     fprintf(stderr, "Usage: %s eingang.dat ausgang.dat [d|f] N M\n", argv[0]);
  9.     return 1;
  10.   }
  11.  
  12.   if((input = fopen(argv[1], "r")) == NULL) {
  13.     fprintf(stderr, "Cannot open input file.\n");
  14.     goto error_input;
  15.   }
  16.  
  17.   if((output = fopen(argv[2], "w")) == NULL) {
  18.     fprintf(stderr, "Cannot open output file.\n");
  19.     goto error_output;
  20.   }
  21.  
  22.   format = strcmp(argv[3], "d"); /* 0 is double, 1 is float */
  23.   N = atoi(argv[4]), M = atoi(argv[5]);
  24.  
  25.   if(format == 0) {
  26.     double* data = (double*) malloc(sizeof(double) * M * N);
  27.     fread(data, sizeof(double), M * N, input);
  28.  
  29.     for(i = 0; i < M; i++) {
  30.       for(j = 0; j < N; j++) {
  31.         fprintf(output, "%f", data[j + i * N]);
  32.         fprintf(output, (j == N - 1) ? "\n" : "\t");
  33.       }
  34.     }
  35.   } else {
  36.     float* data = (float*) malloc(sizeof(float) * M * N);
  37.     fread(data, sizeof(float), M * N, input);
  38.  
  39.     for(i = 0; i < M; i++) {
  40.       for(j = 0; j < N; j++) {
  41.         fprintf(output, "%f", data[j + i * N]);
  42.         fprintf(output, (j == N - 1) ? "\n" : "\t");
  43.       }
  44.     }
  45.   }
  46.  
  47.   fclose(output);
  48. error_output:
  49.   fclose(input);
  50. error_input:
  51.  
  52.   return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement