Changing array of Objects to Pointer Of objects.

Hi Team,


I have written this small program:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//============================================================================
// Name        : ReadingTxtFileDelimitedby~UsingStruct.cpp
// Author      : AJ
// Version     :
// Copyright   : Your copyright notice
// Description : 
//============================================================================

#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAX_LINE_SIZE 300
#define MAX_CLUSTER_PARAM 10
#define MAX_FILE_RECORD_SIZE 100

struct Totals
{
	double call;
	double bill_qty;
	double charge;
	double int_charge;
	double credit_charge;
	double service_charge;
	Totals(double m_call = 0.0,
		   double m_bill_qty = 0.0,
		   double m_charge = 0.0,
		   double m_int_charge = 0.0,
		   double m_credit_charge = 0.0,
		   double m_service_charge = 0.0) :
			   call(m_call),
			   bill_qty(m_bill_qty),
			   charge(m_charge),
			   int_charge(m_int_charge),
			   credit_charge(m_credit_charge),
			   service_charge(m_service_charge) {}
};

struct AggregatedKeys{
	int cluster_id;
	char cluster_param[MAX_CLUSTER_PARAM];

	AggregatedKeys(){
		cluster_id = 0;
		cluster_param[0] = 0; // Default Value
	}
};

struct FileData{
	Totals totals;
	AggregatedKeys keys;
};

int readTxtFile( ){
	FileData fileData[MAX_FILE_RECORD_SIZE];
	int i=0;
	char line[MAX_LINE_SIZE];
	const char filename[] = "C:/file1.txt";
	FILE *fp = fopen(filename, "r");

	try{
		if( fp !=NULL ){
	    	while( !feof(fp) ){
	    	   if ( fgets(line, sizeof line, fp) == NULL )
	    		   break;
	    	   if ( sscanf( line, "%d~%lf~%lf~%lf~%lf~%lf~%lf~%s",
	    			       &fileData[i].keys.cluster_id,
	    			       &fileData[i].totals.call,
	    			       &fileData[i].totals.bill_qty,
	    			       &fileData[i].totals.charge,
	    			       &fileData[i].totals.int_charge,
	    			       &fileData[i].totals.credit_charge,
	    			       &fileData[i].totals.service_charge,
	    			        fileData[i].keys.cluster_param ) == 8 )

	    	   cout<<" Record ::"<<i<<endl;
	    	   cout<<line<<endl;
	    	   printf("Cluster ID :: %d \n", fileData[i].keys.cluster_id);
	    	   printf("Call :: %lf\n", fileData[i].totals.call);
	    	   printf("Billing Quantity :: %lf \n", fileData[i].totals.bill_qty);
	    	   printf("Charge :: %lf \n", fileData[i].totals.charge);
	    	   printf("Initial Charge ::%lf \n", fileData[i].totals.int_charge);
	    	   printf("Credit Charge :: %lf \n", fileData[i].totals.credit_charge);
	    	   printf("Service Charge :: %lf \n", fileData[i].totals.service_charge);
	    	   printf("Cluster Parameter :: %s \n", fileData[i].keys.cluster_param);
	    	   cout<<"**************************************"<<endl<<endl;
	    	   ++i;
	        } //while ends
	    	if( feof(fp )){
	    		cout<<"finished all line reading of file."<<endl;
	    		return 0;
	    	}
	    } //if ends

		else{
			throw 200;
		}
	} // try ends
	catch(int i){
		cout<<"Error Code :: "<<i<<endl;
		cout<<"Unexpected error while file opening."<<endl;
		return 1;
	}
	catch(...){
		cout<<"Unexpected Error"<<endl;
		return 1;
	}
	fclose(fp);
    return 0;
}

int main(){
	readTxtFile();
	return 0;
}

/* OUTPUT::
 Record ::0
15~15.0~52.9~92.1~23.0~23.0~24.0~"ABVNDJ"

Cluster ID :: 15
Call :: 15.000000
Billing Quantity :: 52.900000
Charge :: 92.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::1
25~25.0~52.9~92.1~23.0~23.0~64.0~"AVVNDJ"

Cluster ID :: 25
Call :: 25.000000
Billing Quantity :: 52.900000
Charge :: 92.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 64.000000
Cluster Parameter :: "AVVNDJ"
**************************************

 Record ::2
35~35.0~82.9~82.1~23.0~23.0~54.0~"ABVNDJ"

Cluster ID :: 35
Call :: 35.000000
Billing Quantity :: 82.900000
Charge :: 82.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 54.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::3
45~45.0~62.9~72.1~23.0~23.0~44.0~"EBVNDJ"

Cluster ID :: 45
Call :: 45.000000
Billing Quantity :: 62.900000
Charge :: 72.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 44.000000
Cluster Parameter :: "EBVNDJ"
**************************************

 Record ::4
55~55.0~92.9~62.1~23.0~23.0~24.0~"ABVNTJ"

Cluster ID :: 55
Call :: 55.000000
Billing Quantity :: 92.900000
Charge :: 62.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "ABVNTJ"
**************************************

 Record ::5
65~65.0~82.9~42.1~23.0~23.0~74.0~"ABVNDJ"

Cluster ID :: 65
Call :: 65.000000
Billing Quantity :: 82.900000
Charge :: 42.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 74.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::6
75~75.0~72.9~52.1~23.0~23.0~64.0~"UBVNDJ"

Cluster ID :: 75
Call :: 75.000000
Billing Quantity :: 72.900000
Charge :: 52.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 64.000000
Cluster Parameter :: "UBVNDJ"
**************************************

 Record ::7
95~85.0~62.9~82.1~23.0~23.0~44.0~"ABVUDJ"

Cluster ID :: 95
Call :: 85.000000
Billing Quantity :: 62.900000
Charge :: 82.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 44.000000
Cluster Parameter :: "ABVUDJ"
**************************************

 Record ::8
16~95.0~52.9~52.1~23.0~23.0~34.0~"AIVNDJ"

Cluster ID :: 16
Call :: 95.000000
Billing Quantity :: 52.900000
Charge :: 52.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 34.000000
Cluster Parameter :: "AIVNDJ"
**************************************

 Record ::9
17~45.0~52.9~72.1~23.0~23.0~24.0~"HBVNDJ"
Cluster ID :: 17
Call :: 45.000000
Billing Quantity :: 52.900000
Charge :: 72.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "HBVNDJ"
**************************************

finished all line reading of file.
 */


Above mentioned code is compling fine and giving me expected results.

I am interested to replace fileData[](arrary of Objects) to *fileData(pointer of objects).
But I still want to use sscanf() for reading line by line.Which is used for reading formatted data from string(line in this case.).

Can you please suggest me if I can do this?

Thanks and Regards,
AJ
If you want to run the program at your end.
File Name, Path and Content: C:/file1.txt
1
2
3
4
5
6
7
8
9
10
15~15.0~52.9~92.1~23.0~23.0~24.0~"ABVNDJ"
25~25.0~52.9~92.1~23.0~23.0~64.0~"AVVNDJ"
35~35.0~82.9~82.1~23.0~23.0~54.0~"ABVNDJ"
45~45.0~62.9~72.1~23.0~23.0~44.0~"EBVNDJ"
55~55.0~92.9~62.1~23.0~23.0~24.0~"ABVNTJ"
65~65.0~82.9~42.1~23.0~23.0~74.0~"ABVNDJ"
75~75.0~72.9~52.1~23.0~23.0~64.0~"UBVNDJ"
95~85.0~62.9~82.1~23.0~23.0~44.0~"ABVUDJ"
16~95.0~52.9~52.1~23.0~23.0~34.0~"AIVNDJ"
17~45.0~52.9~72.1~23.0~23.0~24.0~"HBVNDJ"
hey,
in c++ there's actually a class template that improves upon c-style arrays.
try using :
vector<FileData> fileData;

you can then use fileData[n] to get the n-th fileData member. you can add members by calling
fileData.push_back( a new FileData object )
this vector template dynamically resizes so you don't need to worry about specifying MAX_FILE_RECORD_SIZE

don't forget to include <vector> if you want to use it. you can leave your sscanf function unchanged in this case

if you want to use c style pointers, then you will need to first define a pointer to a FileData object:

FileData * fileData;

then you would call malloc to set aside enough memory to hold all the objects:
 
fileData = malloc(sizeof(FileData) * MAX_FILE_RECORD_SIZE)


to access the n-th member you could write *(fileData + n)

so your sscanf function would become sscanf("formatted string", &(*(fileData + n).keys.cluster_id, ....);

you could also use malloc and realloc to dynamically allocate memory as you read in entries from the file, but that would be a bit involved. i think using <vector> is the easiest solution
Last edited on
@Slicedpan.
First of all..Thanks to U. :-)

U right! STL much efficient but I dont want to write this I/O operations using STL.

Your second solution is fulfilling my requirement but Still its using MAX_FILE_RECORD_SIZE value to allocate the memory.I am trying to eliminate the usage of MAX_FILE_RECORD_SIZE.

Let me check and get back to you how did it go.I am getting some other idea too.

Thanks and Regards,
AJ

Hi Team,

Actually this approach will give me rid of this variable MAX_FILE_RECORD_SIZE. In below mentioned for loop i will read in batch of 5. So I will not be in need of know number of records in the file.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//============================================================================
// Name        : 
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : 
//============================================================================

#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAX_LINE_SIZE 300
#define MAX_CLUSTER_PARAM 10
#define MAX_FILE_RECORD_READING_SIZE 5

struct Totals
{
	double call;
	double bill_qty;
	double charge;
	double int_charge;
	double credit_charge;
	double service_charge;
	Totals(double m_call = 0.0,
		   double m_bill_qty = 0.0,
		   double m_charge = 0.0,
		   double m_int_charge = 0.0,
		   double m_credit_charge = 0.0,
		   double m_service_charge = 0.0) :
			   call(m_call),
			   bill_qty(m_bill_qty),
			   charge(m_charge),
			   int_charge(m_int_charge),
			   credit_charge(m_credit_charge),
			   service_charge(m_service_charge) {}
};

struct AggregatedKeys{
	int cluster_id;
	char cluster_param[MAX_CLUSTER_PARAM];

	AggregatedKeys(){
		cluster_id = 0;
		cluster_param[0] = 0; // Default Value
	}
};

struct FileData{
	Totals totals;
	AggregatedKeys keys;
};

int readTxtFile( ){
	FileData *fileData;
	int i=1;
	char line[MAX_LINE_SIZE];
	const char filename[] = "C:/file1.txt";
	FILE *fp = fopen(filename, "r");
	if( fp !=NULL ){
	    fileData = ( FileData* ) malloc(sizeof(FileData)*MAX_FILE_RECORD_READING_SIZE);
		try{
		    while( !feof(fp) ){
                for (int k = 0; k<MAX_FILE_RECORD_READING_SIZE; k++ ){
		    	    if ( fgets(line, sizeof line, fp) == NULL )
	    		        break;
	    	        if ( sscanf( line, "%d~%lf~%lf~%lf~%lf~%lf~%lf~%s",
	    			            &fileData->keys.cluster_id,
	    			            &fileData->totals.call,
	    			            &fileData->totals.bill_qty,
	        			        &fileData->totals.charge,
	    	    		        &fileData->totals.int_charge,
	    		    	        &fileData->totals.credit_charge,
	    			            &fileData->totals.service_charge,
	    			             fileData->keys.cluster_param ) == 8 )

	    	    cout<<" Record ::"<<i<<endl;
	    	    cout<<line<<endl;
	    	    printf("Cluster ID :: %d \n", fileData->keys.cluster_id);
	    	    printf("Call :: %lf\n", fileData->totals.call);
	    	    printf("Billing Quantity :: %lf \n", fileData->totals.bill_qty);
	    	    printf("Charge :: %lf \n", fileData->totals.charge);
	    	    printf("Initial Charge ::%lf \n", fileData->totals.int_charge);
	    	    printf("Credit Charge :: %lf \n", fileData->totals.credit_charge);
	    	    printf("Service Charge :: %lf \n", fileData->totals.service_charge);
	    	    printf("Cluster Parameter :: %s \n", fileData->keys.cluster_param);
	    	    cout<<"**************************************"<<endl<<endl;
                ++i;
	    	    ++fileData;
                } //for ends.which is reading in batches.
		    } //while ends
	    	if( feof(fp )){
	    		cout<<"finished all line reading of file."<<endl;
	    		return 0;
	    	}
		else{
			throw 200;
		}
	} // try ends
	catch(int i){
		cout<<"Error Code :: "<<i<<endl;
		cout<<"Unexpected error while file opening."<<endl;
		return 1;
	}
	catch(...){
		cout<<"Unexpected Error"<<endl;
		return 1;
	}
    } //if ends
	fclose(fp);
    return 0;
}

int main(){

	readTxtFile();
	return 0;
}

/* OutPUT::
 Record ::1
15~15.0~52.9~92.1~23.0~23.0~24.0~"ABVNDJ"

Cluster ID :: 15
Call :: 15.000000
Billing Quantity :: 52.900000
Charge :: 92.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::2
25~25.0~52.9~92.1~23.0~23.0~64.0~"AVVNDJ"

Cluster ID :: 25
Call :: 25.000000
Billing Quantity :: 52.900000
Charge :: 92.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 64.000000
Cluster Parameter :: "AVVNDJ"
**************************************

 Record ::3
35~35.0~82.9~82.1~23.0~23.0~54.0~"ABVNDJ"

Cluster ID :: 35
Call :: 35.000000
Billing Quantity :: 82.900000
Charge :: 82.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 54.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::4
45~45.0~62.9~72.1~23.0~23.0~44.0~"EBVNDJ"

Cluster ID :: 45
Call :: 45.000000
Billing Quantity :: 62.900000
Charge :: 72.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 44.000000
Cluster Parameter :: "EBVNDJ"
**************************************

 Record ::5
55~55.0~92.9~62.1~23.0~23.0~24.0~"ABVNTJ"

Cluster ID :: 55
Call :: 55.000000
Billing Quantity :: 92.900000
Charge :: 62.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "ABVNTJ"
**************************************

 Record ::6
65~65.0~82.9~42.1~23.0~23.0~74.0~"ABVNDJ"

Cluster ID :: 65
Call :: 65.000000
Billing Quantity :: 82.900000
Charge :: 42.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 74.000000
Cluster Parameter :: "ABVNDJ"
**************************************

 Record ::7
75~75.0~72.9~52.1~23.0~23.0~64.0~"UBVNDJ"

Cluster ID :: 75
Call :: 75.000000
Billing Quantity :: 72.900000
Charge :: 52.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 64.000000
Cluster Parameter :: "UBVNDJ"
**************************************

 Record ::8
95~85.0~62.9~82.1~23.0~23.0~44.0~"ABVUDJ"

Cluster ID :: 95
Call :: 85.000000
Billing Quantity :: 62.900000
Charge :: 82.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 44.000000
Cluster Parameter :: "ABVUDJ"
**************************************

 Record ::9
16~95.0~52.9~52.1~23.0~23.0~34.0~"AIVNDJ"

Cluster ID :: 16
Call :: 95.000000
Billing Quantity :: 52.900000
Charge :: 52.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 34.000000
Cluster Parameter :: "AIVNDJ"
**************************************

 Record ::10
17~45.0~52.9~72.1~23.0~23.0~24.0~"HBVNDJ"
Cluster ID :: 17
Call :: 45.000000
Billing Quantity :: 52.900000
Charge :: 72.100000
Initial Charge ::23.000000
Credit Charge :: 23.000000
Service Charge :: 24.000000
Cluster Parameter :: "HBVNDJ"
**************************************

finished all line reading of file.
 */


Thanks to Slicedpan again. :-)

Thanks and Regards,
AJ
Topic archived. No new replies allowed.