-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_c_file.c
executable file
·87 lines (68 loc) · 2.47 KB
/
access_c_file.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
FILE *matrix_file;
char matrix_filename[255];
void read_c_matrix_(char *filename, double *matrix, int *elements) {
/* Small routine to read a binary C file */
int count, i;
/* fix the received filename */
for (i=0; filename[i] != ' '; i++) ;
filename[i] = '\0';
matrix_file = fopen(filename, "r");
if (!matrix_file) {
fprintf(stderr, "Unable to open %s!\n", filename);
return;
}
count = fread(matrix, sizeof(double), *elements, matrix_file);
printf("Succesfully read %i elements from %s\n", count, filename);
fclose(matrix_file);
}
void open_c_matrix_(char *filename, char *mode) {
/* opens binary matrix file. Use this
if the matrix is too big to be read all at once
filename -- pointer to a character string terminated by a blank
character
mode -- access mode for file (terminated by a blank). Usually
"r ", or "w "
*/
int i;
/* fix the received filename and mode*/
for (i=0; filename[i] != ' '; i++) ;
filename[i] = '\0';
for (i=0; mode[i] != ' '; i++) ;
mode[i] = '\0';
matrix_file = fopen(filename, mode);
if (!matrix_file) {
fprintf(stderr, "Unable to open %s!\n", filename);
return;
} else printf("Opened %s .\n", filename);
strcpy(matrix_filename, filename);
}
void close_c_matrix_() {
if (!fclose(matrix_file)) printf("Matrix file closed succesfully.\n");
else fprintf(stderr, "Error closing %s.\n", matrix_filename);
}
void read_c_matrix_line_(double *line, int *elements) {
/* Small routine to read a binary C file */
int count, i;
count = fread(line, sizeof(double), *elements, matrix_file);
/*printf("Succesfully read %i elements from %s\n", count, matrix_filename);*/
}
void read_specified_c_matrix_line_(double *line, int *elements, int *lineNum) {
/* Small routine to read a binary C file */
int count, i;
if (fseek(matrix_file,(long)(*elements*sizeof(double) * *lineNum),SEEK_SET)){
fprintf(stderr, "Unable to find specified location in file.\n");
return;
}
count = fread(line, sizeof(double), *elements, matrix_file);
/*printf("Succesfully read %i elements from %s\n", count, matrix_filename);*/
}
void write_c_matrix_line_(double *line, int *elements) {
/* Small routine to write a binary C file */
int count, i;
count = fwrite(line, sizeof(double), *elements, matrix_file);
/*printf("Succesfully wrote %i elements to %s\n", count, matrix_filename);*/
}