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.
*/
|