Reading text into integer vector

Hi everybody,

I really need that code,

I have a text file

32 4
12 13 4 12
0 0 0 0 0 3 2 3 4

and I want to convert it vector or array

like A[2]={32,4} B[4]={12,13,4,12} C[9]={0,0,0,0,0,3,2,3,4}

The size of the vector or array can be changed everytime.
Hi Airerdem,

Please find the code below:
Please remeber:
-> There are many methods to approach this,But simplest i got it here.This may not be efficient technique tho.
-> I have assumed few things while writing code.Since it were not mentioned in the post.
-> I suggest you to send backround a bit to undertand the requirement of post.

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


I am sure it will help a little bit at least. :-)

Thanks and Regards,
AJ

You are kidding, right? You are mixing C and C++ I/O (don't do that if you can avoid it!), you've got the length of each line of data hard-coded, the filename hardcoded in the function, etc.

You want simple?

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
#include <algorithm>  // copy()
#include <fstream>    // fstream
#include <iostream>   // cout
#include <iterator>   // istream_iterator, ostream_iterator, back_inserter
#include <sstream>    // istringstream
#include <string>     // string
#include <vector>     // vector
using namespace std;

int main()
  {
  vector <int> A, B, C;

  ifstream f( "foo.txt" );  // use any of the appropriate ways to get a filename here
  string s;
  istringstream iss;

  getline( f, s );
  iss.str( s );
  copy( istream_iterator <int> ( iss ), istream_iterator <int> (), back_inserter( A ) );

  getline( f, s );
  iss.clear();
  iss.str( s );
  copy( istream_iterator <int> ( iss ), istream_iterator <int> (), back_inserter( B ) );
  
  getline( f, s );
  iss.clear();
  iss.str( s );
  copy( istream_iterator <int> ( iss ), istream_iterator <int> (), back_inserter( C ) );

  f.close();

  cout << A.size() << " elements : ";  copy( A.begin(), A.end(), ostream_iterator <int> ( cout, " " ) );  cout << endl;
  cout << B.size() << " elements : ";  copy( B.begin(), B.end(), ostream_iterator <int> ( cout, " " ) );  cout << endl;
  cout << C.size() << " elements : ";  copy( C.begin(), C.end(), ostream_iterator <int> ( cout, " " ) );  cout << endl;

  return 0;  
  }

Done. Short and sweet.

Of course, if you notice, you see that you are repeating the same thing over and over. This is a sign that you can make it better, using functions and types.

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
#include <algorithm>  // copy()
#include <fstream>    // fstream
#include <iostream>   // cout
#include <iterator>   // istream_iterator, ostream_iterator, back_inserter
#include <sstream>    // istringstream
#include <string>     // string
#include <vector>     // vector
using namespace std;

// Our special 'read a line as a vector' object
struct linevector_t: public vector <int> { };

// Cast functions
      linevector_t& linevector(       vector <int> & v ) { return static_cast <      linevector_t&> ( v ); }
const linevector_t& linevector( const vector <int> & v ) { return static_cast <const linevector_t&> ( v ); }

// Input operation to read a single line into a vector <int>
istream& operator >> ( istream& ins, linevector_t& v )
  {
  string s;
  getline( ins, s );
  istringstream iss( s );
  v.clear();
  copy( istream_iterator <int> ( iss ), istream_iterator <int> (), back_inserter( v ) );
  return ins;
  }

// Something useful for our example program
ostream& operator << ( ostream& outs, const linevector_t& v )
  {
  copy( v.begin(), v.end(), ostream_iterator <int> ( outs, " " ) );
  return outs << endl;
  }

int main()
  {
  vector <int> A, B, C;

  ifstream f( "foo.txt" );  // use any of the appropriate ways to get a filename here
  f >> linevector( A );
  f >> linevector( B );
  f >> linevector( C );
  f.close();

  cout << A.size() << " elements: " << linevector( A );
  cout << B.size() << " elements: " << linevector( B );
  cout << C.size() << " elements: " << linevector( C );

  return 0;
  }

At this point, you can even generalize it to work on any kind of vector.

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
#include <algorithm>  // copy()
#include <fstream>    // fstream
#include <iostream>   // cout
#include <iterator>   // istream_iterator, ostream_iterator, back_inserter
#include <sstream>    // istringstream
#include <string>     // string
#include <vector>     // vector
using namespace std;

// Our special 'read a line as a vector' object
template <typename T>
struct linevector_t: public vector <T> { };

// Cast function (mutable object)
template <typename T>
linevector_t <T> &
linevector( vector <T> & v )
  {
  return static_cast <linevector_t <T> &> ( v );
  }

// Cast functions (const object)
template <typename T>
const linevector_t <T> &
linevector( const vector <T> & v )
  {
  return static_cast <const linevector_t <T> &> ( v );
  }

// Input operation to read a single line into a vector <T>
template <typename T>
istream& operator >> ( istream& ins, linevector_t <T> & v )
  {
  string s;
  getline( ins, s );
  istringstream iss( s );
  v.clear();
  copy( istream_iterator <T> ( iss ), istream_iterator <T> (), back_inserter( v ) );
  return ins;
  }

// Something useful for our example program
template <typename T>
ostream& operator << ( ostream& outs, const linevector_t <T> & v )
  {
  copy( v.begin(), v.end() - 1, ostream_iterator <T> ( outs, " " ) );
  return outs << v.back() << endl;
  }

int main()
  {
  vector <int>    A;  // Notice how we can use different types of vectors now...
  vector <short>  B;
  vector <double> C;

  ifstream f( "foo.txt" );  // use any of the appropriate ways to get a filename here
  f >> linevector( A );
  f >> linevector( B );
  f >> linevector( C );
  f.close();

  cout << A.size() << " elements: " << linevector( A );
  cout << B.size() << " elements: " << linevector( B );
  cout << C.size() << " elements: " << linevector( C );

  return 0;
  }

You may even want to add error handling:

30
31
32
33
34
35
36
37
38
39
40
41
// Input operation to read a single line into a vector <T>
template <typename T>
istream& operator >> ( istream& ins, linevector_t <T> & v )
  {
  string s;
  getline( ins, s );
  istringstream iss( s );
  v.clear();
  copy( istream_iterator <T> ( iss ), istream_iterator <T> (), back_inserter( v ) );
  if (!iss.eof()) ins.setstate( ios::badbit );
  return ins;
  }
58
59
  f >> linevector( A );
  if (!f) throw fooey();

Hope this helps.
Topic archived. No new replies allowed.