CONFIG LIB 1.5
Configuration Files Library (by TGG 2020)
Loading...
Searching...
No Matches
txt_out_file.cpp
1/*********************************************************************/
2/* */
3/* Config files library - (C) TGG 2019 */
4/* */
5/*********************************************************************/
6/* Warszawa, 2019 */
7/*********************************************************************/
8/* */
9/* File: txt_out_file.cpp */
10/* */
11/* Author: T.Grabowski */
12/* */
13/* Contents - general output txt file */
14/* */
15/* */
16/*********************************************************************/
17/* */
18/* */
19
20#include "txt_out_file.h"
21
23{
24 iType = iTypeIn;
25 Clean();
26}
27
28TXT_OUT_FILE::~TXT_OUT_FILE( void )
29{
30 CleanTabs();
31}
32
34{
35 nRow = 0;
36 nCol = NCOL_MAX;
37
38 for( int i=0; i<nCol; i++ )sprintf(cName[i],"Var %d",i);
39
40 file_flag = 0;
41 dResult = NULL;
42 iObj = NULL;
43}
44
46{
47 if( dResult )
48 {
49 for( int i=0; i<nCol; i++ )if( dResult[i] ){ delete [] dResult[i]; dResult[i] = NULL; }
50 delete [] dResult;
51 dResult = NULL;
52 }
53 if( iObj )delete [] iObj;
54 iObj = NULL;
55}
56
57void TXT_OUT_FILE::InitTabs( int n, int m )
58{
59 CleanTabs();
60
61 nCol = n;
62 nRow = m;
63 dResult = new double*[nCol];
64 for( int i=0; i<nCol; i++ )dResult[i] = new double[nRow];
65 if ( iType ) iObj = new int[nRow];
66}
67
68void TXT_OUT_FILE::SetName( const char* TXT_file )
69{
70 strcpy( file_name, TXT_file );
71 file_flag = 1;
72}
73
75{
76 return file_name;
77}
78
79int TXT_OUT_FILE::Read( const char* TXT_file )
80{
81 SetName( TXT_file);
82 FILE *File;
83 File = fopen( TXT_file, "r" );
84 if( File == NULL )
85 {
86 fprintf( stderr, "File open error to reading (%s)\n", TXT_file );
87 return (-1);
88 }
89
90 memset( cName, 0, NCOL_MAX*32 );
91 CleanTabs();
92
93 nCol = nColumns( File, cName ) - iType;
94 nRow = nLines( File );
95
96 dResult = new double*[nCol];
97 for( int i=0; i<nCol; i++ )dResult[i] = new double[nRow];
98 iObj = new int[nRow];
99
100 for( int j=0; j<nRow; j++ )
101 {
102 if(iType)fscanf( File, "%d", &iObj[j] );
103 for( int i=0; i<nCol; i++ )fscanf( File, "%lf", &dResult[i][j] );
104 }
105
106 fclose( File );
107
108 return 0;
109}
110
112{
113 if( file_flag == 0 )return -1;
114 return Read( file_name );
115}
116
117int TXT_OUT_FILE::Write( const char* TXT_file )
118{
119 SetName( TXT_file);
120 FILE *File;
121 File = fopen( TXT_file, "w" );
122 if( File == NULL )
123 {
124 fprintf( stderr, "File open error to writing (%s)\n", TXT_file );
125 return (-1);
126 }
127
128 for( int i=0; i<nCol+iType; i++ )fprintf( File, " %s", cName[i] );
129 fprintf( File, "\n" );
130 for( int j=0; j<nRow; j++ )
131 {
132 if(iType)fprintf( File, " %5d", iObj[j] );
133 for( int i=0; i<nCol; i++ )fprintf( File, " %#16.8g", dResult[i][j] );
134 fprintf( File, "\n" );
135 }
136 fclose( File );
137
138 return 0;
139}
140
142{
143 if( file_flag == 0 )return -1;
144 return Write( file_name );
145}
146
static FILE * fopen(const char *filename, const char *mode)
Cross-platform function to fopen function that supports UTF-8 encoded name.
Definition iofun.cpp:358
static int nColumns(FILE *stream, char Par[][32])
Returns number of columns of text file (number of strings in the first line). Names of columns are st...
Definition iofun.cpp:447
static int nLines(FILE *stream)
Returns number of lines of text file from current pointer to EOF.
Definition iofun.cpp:426
virtual int Read(void)
reads the [.czy] file defined by SetName function
int nRow
number of rows
virtual int Write(void)
writes the [.czy] file defined by SetName function
char cName[NCOL_MAX][32]
array of variable names - up to NCOL_MAX names, 32 characters each
int nCol
number of columns/variables (up to NCOL_MAX)
int * iObj
pointer to array [nRow] first column value (int)
virtual char * GetName(void)
returns pathname of the output text file
TXT_OUT_FILE(int iTypeIn=0)
constructor
double ** dResult
pointer to array (size depends on iType) containing the results for all variables
virtual void InitTabs(int n, int m)
initialises arrays (n - number of columns, m - number of rows )
virtual void CleanTabs(void)
cleans (deletes) arrays
virtual void SetName(const char *FileName)
set pathname for of the output text file
virtual void Clean(void)
cleans local variables
int iType
type of txt file - possible types: 0 - nCol x nRow - only double type values 1 - (nCol-1) x nRow ...