35 ifstream fmat(filename.
c_str());
36 if ( fmat.fail() )
return matrix;
39 unsigned long i, j, k, n;
42 while ( fmat.getline(aline, 1024) && strncmp(aline,
"matrix_", 7) ) ;
44 fmat.getline(aline, 1024);
49 cout <<
"DEBUG read_matrix: aline = \"" << aline <<
"\"" << endl;
51 int row_label_flag(0);
54 if ( *labels ==
"#" ) {
57 labels = labels->
next;
61 cerr <<
"Error: No matrix columns found in " << filename << endl;
67 while ( fmat.getline(aline, 1024) && strlen(aline) > cols ) {
69 cout <<
"DEBUG read_matrix: aline = \"" << aline <<
"\"" << endl;
74 if ( n < cols )
break;
87 cout <<
"Reading file " << filename <<
" with a matrix of size " <<
88 matrix.
columns() <<
"x" << matrix.
rows() << endl << endl;
92 while ( fmat.getline(aline, 1024) && strncmp(aline,
"matrix_", 7) ) ;
94 fmat.getline(aline, 1024);
97 while ( fmat.getline(aline, 1024) && i < rows ) {
101 if ( row_label_flag ) {
102 alabel = sline.
pre(
' ');
108 cerr <<
"Row " << i+1 <<
" contains only " << n <<
" values in stead of " << cols <<
" values!" << endl;
109 for ( j=0; j<cols; j++, k++ ) matrix[i*cols+j] = temp[k];
114 cerr <<
"The matrix contains only " << i <<
" rows in stead of " << rows <<
" rows!" << endl;
134 ofstream fmat(filename.
c_str());
135 if ( fmat.fail() )
return -1;
138 cout <<
"Writing file " << filename <<
" with a matrix of size " <<
139 matrix.
columns() <<
"x" << matrix.
rows() << endl << endl;
143 fmat <<
"# Matrix written by Bsoft\n\ndata_\n\nmatrix_" << endl;
147 fmat <<
" " << *label;
150 for ( i=k=0, label=matrix.
row_labels(); i<matrix.
rows(); i++, label=(label)?label->
next:NULL ) {
151 if ( label ) fmat << *label;
152 for ( j=0; j<matrix.
columns(); j++, k++ )
153 fmat <<
" " << matrix[k];
Bstring substr(long start, long len)
Header file for general utilities.
long count_list(char *list)
Counts the number of items in a linked list.
Bmatrix read_matrix(Bstring &filename)
Reads a matrix file.
Bstring * column_labels()
Header file for generalized linked list functions.
Header file for reading (and writing) matrices.
float * split_into_floats(const Bstring &delim, unsigned long &n)
void row_add_label(Bstring &s)
int bexit(int value)
Exit function for cleanup and debugging.
int write_matrix(Bstring &filename, Bmatrix matrix)
Writes a matrix file.
int string_kill(Bstring *list)
Kills a string linked list.