Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(int argc, char** argv) {
- FILE *input, *output;
- int format, N, M, i, j;
- if(argc != 6 || (strcmp(argv[3], "d") && strcmp(argv[3], "f"))) {
- fprintf(stderr, "Usage: %s eingang.dat ausgang.dat [d|f] N M\n", argv[0]);
- return 1;
- }
- if((input = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "Cannot open input file.\n");
- goto error_input;
- }
- if((output = fopen(argv[2], "w")) == NULL) {
- fprintf(stderr, "Cannot open output file.\n");
- goto error_output;
- }
- format = strcmp(argv[3], "d"); /* 0 is double, 1 is float */
- N = atoi(argv[4]), M = atoi(argv[5]);
- if(format == 0) {
- double* data = (double*) malloc(sizeof(double) * M * N);
- fread(data, sizeof(double), M * N, input);
- for(i = 0; i < M; i++) {
- for(j = 0; j < N; j++) {
- fprintf(output, "%f", data[j + i * N]);
- fprintf(output, (j == N - 1) ? "\n" : "\t");
- }
- }
- } else {
- float* data = (float*) malloc(sizeof(float) * M * N);
- fread(data, sizeof(float), M * N, input);
- for(i = 0; i < M; i++) {
- for(j = 0; j < N; j++) {
- fprintf(output, "%f", data[j + i * N]);
- fprintf(output, (j == N - 1) ? "\n" : "\t");
- }
- }
- }
- fclose(output);
- error_output:
- fclose(input);
- error_input:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement