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
|
#include <stdio.h>
#include <iostream>
#define MAX_LINE_SIZE 200
using namespace std;
int readTxtFile( ){
int A[2], B[4], C[9];
try{
//int status =0;
char line[MAX_LINE_SIZE];
const char filename[] = "C:/file2.txt";
FILE *fp = fopen(filename, "r");
if( fp !=NULL ){
if ( fgets(line, sizeof line, fp) == NULL )
throw 300;
if ( sscanf( line, "%d %d",
&A[0],
&A[1] ) == 2 )
if ( fgets(line, sizeof line, fp) == NULL )
throw 400;
if ( sscanf( line, "%d %d %d %d",
&B[0],
&B[1],
&B[2],
&B[3]) == 4 )
if ( fgets(line, sizeof line, fp) == NULL )
throw 500;
if ( sscanf ( line,"%d %d %d %d %d %d %d %d %d",
&C[0],
&C[1],
&C[2],
&C[3],
&C[4],
&C[5],
&C[6],
&C[7],
&C[8]
) == 9)
cout<<"Elements of A ::"<<endl;
for( int j = 0; j< 2; j++)
cout<<"A["<<j<<"] - "<<A[j]<<endl;
cout<<"Elements of B ::"<<endl;
for( int k = 0; k< 4; k++)
cout<<"B["<<k<<"] - "<<B[k]<<endl;
cout<<"Elements of C ::"<<endl;
for (int l =0; l<9; l++)
cout<<"C["<<l<<"] - "<<C[l]<<endl;
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;
}
return 0;
}
int main(){
readTxtFile();
return 0;
}
/* OutPUT::
Elements of A ::
A[0] - 32
A[1] - 4
Elements of B ::
B[0] - 12
B[1] - 13
B[2] - 4
B[3] - 12
Elements of C ::
C[0] - 0
C[1] - 0
C[2] - 0
C[3] - 0
C[4] - 0
C[5] - 3
C[6] - 2
C[7] - 3
C[8] - 4
finished all line reading of file.
*/
/*
c:/file2.txt is this:
32 4
12 13 4 12
0 0 0 0 0 3 2 3 4
*/
|