how to read data from a textfile and convert the numbers into array

I am trying to read from a file created by openFOAM.
the first 18 lines are not interesting, in line 19 is a number that tells the size of the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
2000
(
0
0
0
0
0
0
0
0
0
0
0

my code so far is
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
#include <iostream>  							
#include <fstream>								
#include <string>								
using namespace std;							

int main () {
  string line;
  ifstream inlet ("InletPeak");
  if (inlet.is_open())
  {
      while ( inlet.good() )						
			for ( int i=0;!inlet.eof();i=i+1)          		
		{
                  if (i==19)
          {
        getline(inlet, s);       				
        cout << s << endl;    					
        }
        else {inlet.eof();
             getline(inlet,n);
             }
    }
	    {
      getline (inlet,line);
	        cout << line << endl;
    }
    inlet.close();
  }
  else cout << "Unable to open file"; 
  return 0;
}

but as far as i know line contains now a string. how do i read it as a int so that i can create a array to store all the following numbers?

This sounds similar to something I was trying to do recently. Here is the thread;

http://www.cplusplus.com/forum/beginner/59195/

and here is the code that I found worked (I know its slightly different in your case as you don't want the first 18 numbers but maybe it will help :-))

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
#include <iostream>
#include <fstream>
#include <cerrno>
#include <vector>

using namespace std;

std::vector<double> read_from_file()
{
std::ifstream file("myfile.txt");
std::vector<double> numbers;

double n;
while( file >> n ) numbers.push_back(n) ;

return numbers;
}

int main ()
{
vector<double> printnumbers;
printnumbers = read_from_file();
cout << printnumbers[0] << "\n" ;

return 0;
}
hi,
i improved the code, so at least the size of the vector is recognized. but the programm is not reading the numbers, but outputs 1.38498e+219 instead of 0
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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
    ifstream inlet("testmitopenfoamhaeder.txt");
    if(inlet.is_open()){
        cout << "File ist vorhanden und zuweisung hat funktioniert"<< endl;
            int laenge;
            char asdf[80];
            for ( int i=0;!inlet.eof();i=i+1)
            {
            inlet.getline(asdf, 80);
            if (i==17){
                inlet >> laenge;
                break;}
            }
        cout << laenge << endl;

        vector<double> inletpeak(laenge);
            for ( int i=0;!inlet.eof();i=i+1)
            {
            inlet.getline(asdf, 80);
            if (i>18){
                inlet >> inletpeak[i-19];
                }
            cout << "test" << inletpeak[i-19] << endl;
            }
    }
    else { cout << "Dateizuordnung hat nicht funktioniert"<<endl;}
}

and the testfile looks like:
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
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  1.7.1                                 |
|   \\  /    A nd           | Web:      www.OpenFOAM.com                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       scalarField;
    location    "constant";
    object      InletPeak;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //


10
(
0
0
0
0
0
0
0
0
0
0
)

has anyone an idea of what is going wrong?
thx
Topic archived. No new replies allowed.