Exercise 2 for the course "Parallel and distributed systems" of THMMY in AUTH university.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

6 years ago
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void writeTotxt(int rows, int columns, double array[rows][columns], char *filename,
int commaSeperated, int tabSeperated);
int main(int argc, char const *argv[]){
double **pointsArray;
char binaryFilename[200], textFilename[200];
int commaSeperated = 0, tabSeperated = 0;
int seperationMethod, numberOfRows, numberOfColumns;
printf("Seperate values with:\n\t[0] Space (default)\n\t[1] Comma\n\t[2] Tab\nInput = ");
scanf("%d", &seperationMethod);
switch(seperationMethod){
case 1 :
commaSeperated = 1;
break;
case 2 :
tabSeperated = 1;
break;
}
printf("\nArray rows = ");
scanf("%d", &numberOfRows);
printf("\nArray columns = ");
scanf("%d", &numberOfColumns);
pointsArray = (double **)(malloc(numberOfRows * sizeof(double *)));
for (int i = 0; i < numberOfRows; ++i){
pointsArray[i] = (double *)(malloc(numberOfColumns * sizeof(double)));
}
printf("\nBinary filename = ");
scanf("%s", binaryFilename);
printf("\nText filename = ");
scanf("%s", textFilename);
FILE *pointsBinaryFile;
pointsBinaryFile = fopen(binaryFilename,"rb");
if(fread(&pointsArray, sizeof(double), numberOfRows * numberOfColumns,
pointsBinaryFile) != numberOfRows * numberOfColumns) {
if(feof(pointsBinaryFile))
printf("There were usefull info after the end of file.\n");
else
printf("File read error.\n");
return 1;
}
fclose(pointsBinaryFile);
for (int i = 0; i < numberOfRows; ++i){
for (int j = 0; j < numberOfColumns; ++j){
printf("%f\n", pointsArray[i][j]);
}
}
FILE *file = fopen(textFilename, "w");
if (file == NULL){
printf("Error opening file!\n");
exit(1);
}
for (int i = 0; i < numberOfRows; ++i){
for (int j = 0; j < numberOfColumns; ++j){
fprintf(file, "%f", pointsArray[i][j]);
if (j != numberOfColumns - 1){
if (commaSeperated){
fprintf(file, ", ");
} else if (tabSeperated){
fprintf(file, "\t");
} else {
fprintf(file, " ");
}
}
}
fprintf(file, "\n");
}
fclose(file);
printf("Done! Press any key to exit.\n");
getchar();
}