-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact-with-file-system.cpp
72 lines (65 loc) · 1.66 KB
/
interact-with-file-system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include "structures.h"
int readFile (char* fileName, double initArray[]) {
FILE* file = fopen(fileName, "r");
struct params {
char name[20];
double value;
};
struct params option[10];
int n=0;
if (file == NULL) {
printf("File not found!");
}
else {
while (fscanf (file, "%s%lf", option[n].name, &(option[n].value)) != EOF) {
printf("%s %.2f\n", option[n].name, option[n].value);
initArray[n] = option[n].value;
n++;
}
fclose(file);
}
return 0;
}
int writeFile (double A[], int N, char* option) {
FILE* file = fopen("output.txt", option);
for (int i = 0; i < N; i++)
{
fprintf(file, "%d\t%f\n", i, A[i]);
}
fprintf(file, "\n");
fclose(file);
return 0;
}
int writeMultiCols (double** A, parameters var, double dt, int cases, int N, char* option) {
FILE* file = fopen("output.txt", option);
switch (cases)
{
case 0:
if (*option == 'w')
{
fprintf(file, "num\tU\tvelocity\ttime\n");
}
for (int i = 0; i < N; i++)
{
fprintf(file, "%d\t%.2f\t%.2f\t%f\n",
i, A[0][i], var.velocity[i], dt);
}
break;
case 1:
if (*option == 'w')
{
fprintf(file, "num\trho\tvelocity\tpressure\tenergy\ttime\n");
}
for (int i = 0; i < N; i++)
{
fprintf(file, "%d\t%.2f\t%.2f\t%.2f\t%.2f\t%f\n",
i, A[0][i], var.velocity[i], var.pressure[i], var.energy[i], dt);
}
default:
break;
}
fprintf(file, "\n");
fclose(file);
return 0;
}