Filling Arrays from a Text File

I am having a trivial trouble on how to create three different arrays from a text file. I am a beginner in C++.

I have a .txt file containing a string of 'float' values as below:

0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3

//----------------------

Now, I want to make three arrays, p1[], p2[], and p3[] from them, so that
p1[] has elements from line: 1, 4, 7, ...,
p2[] has elements from line: 2, 5, 8, .., and
p3[] has elements from line: 3, 6, 9,... of the .txt file.

My original file has a huge amount of data, so I would have to use a loop, but I cannot think of a way to fill my arrays as described.

Can anyone help me please? I would appreciate greatly.

Holly sh, it is working. This is weird syntax. Okay, I figured it out.
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
/*

 */


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>

void main()
{
  float data1[5], data2[5], data3[5];

  ifstream myfile("text.txt");

  if(!myfile) //Check if a file is open
    {
      cout << "Error opening file." << endl;
    }

  int a = 0;
  
  while(!myfile.eof() && a<5)
    {
      myfile >> data1[a];
           
      myfile >> data2[a];
     
      myfile >> data3[a];   

      a+=1;
      
    }

  //Check if arrays are created
  for(int iii=0; iii<5; iii++)
    {
      cout << "data1[" << iii << "] = " << data1[iii] << endl;
    }
  for(int iii=0; iii<5; iii++)
    {
      cout << "data2[" << iii << "] = " << data2[iii] << endl;
    }
  for(int iii=0; iii<5; iii++)
    {
      cout << "data3[" << iii << "] = " << data3[iii] << endl;
    }
}

Last edited on
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 <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>

typedef std::vector<double> vec_type ;

void print(const vec_type& a, const vec_type& b, const vec_type& c)
{
    const unsigned colWidth = 10 ;

    std::cout << std::setw(colWidth/2) << 'a' 
              << std::setw(colWidth) << 'b' 
              << std::setw(colWidth) << 'c' << '\n';

    char oldFill = std::cout.fill('_') ;
    std::cout << std::setw(colWidth*3) << "" << '\n' ;
    std::cout.fill(oldFill) ;

    for ( unsigned i=0; i<a.size(); ++i )
        std::cout << std::setw(colWidth) << a[i] 
                  << std::setw(colWidth) << b[i] 
                  << std::setw(colWidth) << c[i] << '\n' ;
}

int main()
{
    vec_type p1, p2, p3 ;

//    std::ifstream in("filename") ;
    std::istringstream in( "0.5\n0.6\n0.7\n0.8\n0.9\n1.0\n1.1\n1.2\n1.3\n") ;

    double a, b, c ;
    while ( in >> a >> b >> c )
        p1.push_back(a), p2.push_back(b), p3.push_back(c) ;

    print(p1, p2, p3) ;
}
closed account (D80DSL3A)
Use an array of pointers?
1
2
3
float *ppf[] = { p1, p2, p3 };
unsigned i = 0;
while( myFile >> ppf[i%3][i/3] ) ++i;
Last edited on
Topic archived. No new replies allowed.