How to read data in files and extract numbers?


I have some data stored in a file formatted like this (yes it is a result from OpenFOAM):

FoamFile
{
version 2.0;
format ascii;
class volVectorField;
location "401";
object U;
}

dimensions [0 1 -1 0 0 0 0];

internalField nonuniform List<vector>
4378121
(
(1.01069 0.0216525 -0.00441842) // start line
(1.00634 0.00667446 -0.000859575)
(1.00557 0.00505487 -0.000599214)
(1.00487 0.00373986 -0.000414328)

......

(1.00213 6.22804e-05 4.43194e-06) // end line
)
;


the numbers in the parentheses are what I want. I hope to use some c++ code to extract the first component to do some calculations beginning from the first vector to the last one. I have hundreds files like this and each file has 4 millions lines.

I get stuck with this problem for a time and haven't figured out how to do

I dont have a good C++ background (and also don't want to use OpenFOAM default reading routine to do this because it causes another problem)

Could anyone give any hints or wonderful solutions? Just tell me how to deal with one line
@dongwang, how did you manage to run OpenFOAM without knowing much c++?

Read each line into a string using getline(). Ignore them all until you come to the one with the "internalField" indicator.
After reading the next line into a string, stringstream it into an int N, the number of data points.
Skip the next line if it's just a (.
Then loop through the next N lines, stringstreaming the result of getline() into char, double, double, double. Those doubles will be the velocity vector components that you want. You can process them however you want as soon as you've read them.

Here's a basic example that simply reads the file and writes the velocity vectors to screen.

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

void readFoamFile( istream &strm )
{
   string line;
   int N;


   // Read lines; the while loop will stop when it contains "internalField"
   while ( getline( strm, line ) && line.find( "internalField" ) == string::npos ) ;


   getline( strm, line );   stringstream( line ) >> N;     // Get the number of data points

   getline( strm, line );                                  // Ignore the next line


   // Loop through the data points, grabbing the velocity vector (U,V,W)
   char c;
   double u, v, w;
   for ( int i = 0; i < N; i++ )
   {
      getline( strm, line );
      stringstream( line ) >> c >> u >> v >> w;

      // Do whatever you want with u, v, w; I'll just write to screen
      cout << u << "  " << v << "  " << w << '\n';
   }
}


int main()
{
   string filename = "foamFile";                     // or whatever

// ifstream strm( filename );                        // You want to read from file

   stringstream strm( "FoamFile                 \n"  // I'll fake it with a stringstream
                      "{                        \n"
                      "version 2.0;                         \n"
                      "format ascii;                        \n"
                      "class volVectorField;                \n"
                      "location 401;                        \n"
                      "object U;                            \n"
                      "}                                    \n"
                      "                                     \n"
                      "dimensions [0 1 -1 0 0 0 0];         \n"
                      "                                     \n"
                      "internalField nonuniform List<vector>\n"
                      "5                                    \n"
                      "(                                    \n"
                      "(1.01069 0.0216525 -0.00441842)      \n"
                      "(1.00634 0.00667446 -0.000859575)    \n"
                      "(1.00557 0.00505487 -0.000599214)    \n"
                      "(1.00487 0.00373986 -0.000414328)    \n"
                      "(1.00213 6.22804e-05 4.43194e-06)    \n"
                      ")                                    \n"
                      ";                                    \n" );

   readFoamFile( strm );
}


1.01069  0.0216525  -0.00441842
1.00634  0.00667446  -0.000859575
1.00557  0.00505487  -0.000599214
1.00487  0.00373986  -0.000414328
1.00213  6.22804e-005  4.43194e-006



That great repository of everything CFD, CFD Online, also has a forum devoted to OpenFOAM:
https://www.cfd-online.com/Forums/openfoam/
Last edited on
@lastchangce thank you for your reply

I am learning OpenFOAM and C++ and for the moment I am very rookie. but sometimes you have to finish some work before deadline without enough knowledge

You idea helps a lot thanks again
Topic archived. No new replies allowed.