help for save many data into array

Pages: 12
please helppp.

i have data saved in file data.txt
63.067098 128.986908 0.89787897 0.97687987 0.677647643
63.067045 128.986912 0.89787812 0.97687943 0.677647433
63.067034 128.984312 0.89787834 0.97687953 0.677647436
63.067052 128.986963 0.89787853 0.97687956 0.677647443

!!the data until more than 10.000 (data i cant write it here.)

so my question is how to save the data into a multidimensional array ?
i have so many rows so i dont know to determine size of the array.

i already try this

void savedataintoarray()
{
double lat,lon,height,T,v;

ifstream file("data.txt);
if(file.is_open()){
while(myfile>>lat >>lon>>height>>T>>v);{

//save in array
int i;
double lat = lat[i];
for(i=0; i<(???); i++){; //i dont know exactly the size
cout<<lat[i];
}

//just display first column
printf("%.7f\n ,lat);
}
}

help mee, what im supposed to do
Last edited on
First of all, ask yourself whether you actually need to store all that data. For example, you don't need to store anything other than a running sum and a counter just to add up or average the first column.

However, presuming that you want to do more than that, then the container for storing an unknown and expansible amount of objects is a vector.

Here, your input statement implies that the columns of data have some specific meanings, so one row of data could be stored in a struct (called "Item" below) with those individual elements inside. Then the whole would be a vector<Item> as below.

For convenience only I have defined >> and << operators for Item in the following code. The output operator << in particular can be tailored to whatever you want.

Why are all your data points in the middle of Russia, BTW?

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

struct Item
{
   double latitude;
   double longitude;
   double height;
   double T;
   double v;
};

istream &operator >> ( istream& strm, Item &e ) { return strm >> e.latitude >> e.longitude >> e.height >> e.T >> e.v; }

ostream &operator << ( ostream& strm, Item e  )
{
   # define SP << '\t' << setw( 15 ) << setprecision( 8 ) <<
   return strm SP e.latitude SP e.longitude SP e.height SP e.T SP e.v;
}

void readData( string filename, vector<Item> &V );

//============================================================

int main()
{
   vector<Item> V;
   readData( "data.txt", V );

   cout << "All data:\n";
   for ( Item e : V ) cout << e << '\n';
  
   cout << "\nFirst column only:\n";
   for ( Item e : V ) cout << e.latitude << '\n';
}

//============================================================

void readData( string filename, vector<Item> &V )
{
   Item thing;
   fstream in( filename );
   while ( in >> thing ) V.push_back( thing );
   in.close();
}

//============================================================ 


All data:
	      63.067098	      128.98691	     0.89787897	     0.97687987	     0.67764764
	      63.067045	      128.98691	     0.89787812	     0.97687943	     0.67764743
	      63.067034	      128.98431	     0.89787834	     0.97687953	     0.67764744
	      63.067052	      128.98696	     0.89787853	     0.97687956	     0.67764744

First column only:
63.067098
63.067045
63.067034
63.067052


Last edited on
Thank you very much for your kind replied.
It's complicated that i though. i think i can just save it in an array and call them again.
At least i need 3 data latitude, longitude and a parameter. i want to make grid points from the data.
it just an example, i just pick any latitude and longitude my actually data is in japan.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//============================================================

int main()
{
   vector<Item> V;
   readData( "data.txt", V );

   cout << "All data:\n";
   for ( Item e : V ) cout << e << '\n';    //can u help me ? i dont know its a error in here 
  
   cout << "\nFirst column only:\n";
   for ( Item e : V ) cout << e.latitude << '\n';
}

//============================================================ 

Last edited on
You haven't given any information about why you "know it's an error" in that line. It contains a range-based for-loop, so if your compiler flags a compile error there then it is likely that you have not enabled c++11 for your compiler. Either your compiler is very old or you may need to set a switch for c++11.

If, on the other hand, there is simply no data showing then it is possible that you did not find and open the data file. Test the input stream after opening, or simply output V.size() to check.

You simply don't give enough information about your error.
you were right i got old compiler i already changed it to c+11. thank you and i already run it and its work thank you.
but i have another problem my real data is consist from 17 columns so i change the program a little like:

1
2
istream &operator >> ( istream& strm, Item &e ) { return strm >> e.latitude >> e.longitude >> e.height >> e.PSFC >> e.u10>> e.v10>> e.t2 >> e.q2 >>
                                                          e.RAINC >> e.RAINNC >> e.SNOWNC >> e.SWDOWN >> e.GLW >> e.UST >> e.HFX >> e.LH; }


when i run it
1
2
3
4
5
6
7
8
9
All data:
              33.239441         135.194               0       101184.69       3.1869767       4.8853555       298.65564
    0.019259913               0               0               0               0               0               0             0.1               0
                      0        33.23962       135.21576               0       101186.76       3.1119459       4.9476247
      298.65494     0.019257976               0               0               0               0               0               0             0.1
                      0               0       33.239803       135.23755               0       101188.84       3.0464182
      5.0038157       298.65393     0.019255746               0               0               0               0               0               0
                    0.1               0               0       33.239967       135.25934               0       101190.92
      2.9922979       5.0529943       298.65274     0.019254941               0               0               0               0               0

and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
First column only:
33.239441
0
0
0.1
0
0
0
0
0
0
0.019229298
298.62958
5.3496914
2.7867634
101212.55
0
135.52075
33.241741
0
0


why the first column gave me all the data in one column? can you help please??
@Ryoucplus, if you have changed the code and it has ceased to work as a result, please could you post the entire modified code. It would also help if you gave the first few lines of the input file so that I can test it.
i'm sorry here i post it.
data
  33.23944092   135.19400024   0.0000000       101184.69       3.1869767       4.8853555       298.65564      0.19259913E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.23962021   135.21575928   0.0000000       101186.76       3.1119459       4.9476247       298.65494      0.19257976E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.23980331   135.23754883   0.0000000       101188.84       3.0464182       5.0038157       298.65393      0.19255746E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.23996735   135.25933838   0.0000000       101190.92       2.9922979       5.0529943       298.65274      0.19254941E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.24013519   135.28112793   0.0000000       101193.03       2.9476733       5.0988040       298.65079      0.19254250E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.24029922   135.30291748   0.0000000       101195.17       2.9083714       5.1422067       298.64828      0.19251581E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.24045181   135.32470703   0.0000000       101197.33       2.8743668       5.1832223       298.64517      0.19247273E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000    
   33.24060440   135.34646606   0.0000000       101199.52       2.8457768       5.2217307       298.64166      0.19241894E-01   0.0000000       0.0000000       0.0000000       0.0000000       0.0000000       0.0000000      0.10000000       0.0000000       0.0000000 


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
code
struct Item{
double latitude;
   double longitude;
   double height;
   double PSFC;
   double u10;
   double v10;
   double t2;
   double q2;
   double RAINC;
   double RAINNC;
   double SNOWNC;
   double GRAUPELNC;
   double SWDOWN;
   double GLW;
   double UST;
   double HFX;
   double LH;
   
};

istream &operator >> ( istream& strm, Item &e ) { return strm >> e.latitude >> e.longitude >> e.height >> e.PSFC >> e.u10>> e.v10>> e.t2 >> e.q2 >>
                                                          e.RAINC >> e.RAINNC >> e.SNOWNC >> e.SWDOWN >> e.GLW >> e.UST >> e.HFX >> e.LH; }

ostream &operator << ( ostream& strm, Item e  )
{
   # define SP << '\t' << setw( 15 ) << setprecision( 8 ) <<
   return strm SP e.latitude SP e.longitude SP e.height SP e.PSFC SP e.u10 SP e.v10 SP e.t2 SP e.q2 SP e.RAINC SP e.RAINNC SP e.SNOWNC SP e.SWDOWN
               SP e.GLW SP e.UST SP e.HFX SP e.LH;
}

void readData( string filename, vector<Item> &V );

//============================================================

int main()
{
   vector<Item> V;
   readData( "wrfdatacut.txt", V );

   cout << "All data:\n";
   for ( Item e : V ) cout << e << '\n';
  
   cout << "\nFirst column only:\n";
   for ( Item e : V ) printf("%.9f \n ", e.latitude);
   //cout << e.latitude << '\n'; 
}

//============================================================

void readData( string filename, vector<Item> &V )
{
   Item thing;
   fstream in( filename );
   while ( in >> thing ) V.push_back( thing );
   in.close();
}

//============================================================  
@Ryoucplus, please post ALL the code, including the header lines (#include statements especially).

You appear to have omitted to read (or write) one column, GRAUPELNC, which will wreck your data read.

Please also change that printf statement back to a cout.

How many columns of that data do you actually NEED?
Last edited on
Oh you were right i omitted GRAUPELNC. i already run it and it run well, thank you.
i want to ask how to display all of the value number i mean all of the decimal. i can't do it with cout so thats why i changed it into printf.

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
all code
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

struct Item
{
   double latitude;
   double longitude;
   double height;
   double PSFC;
   double u10;
   double v10;
   double t2;
   double q2;
   double RAINC;
   double RAINNC;
   double SNOWNC;
   double GRAUPELNC;
   double SWDOWN;
   double GLW;
   double UST;
   double HFX;
   double LH;
   
};

istream &operator >> ( istream& strm, Item &e ) { return strm >> e.latitude >> e.longitude >> e.height >> e.PSFC >> e.u10>> e.v10>> e.t2 >> e.q2 >>
                                                          e.RAINC >> e.RAINNC >> e.SNOWNC >> e.GRAUPELNC>> e.SWDOWN >> e.GLW >> e.UST >> e.HFX >> e.LH; }

ostream &operator << ( ostream& strm, Item e  )
{
   # define SP << '\t' << setw( 15 ) << setprecision( 8 ) <<
   return strm SP e.latitude SP e.longitude SP e.height SP e.PSFC SP e.u10 SP e.v10 SP e.t2 SP e.q2 SP e.RAINC SP e.RAINNC SP e.SNOWNC SP e.GRAUPELNC 
            SP e.SWDOWN SP e.GLW SP e.UST SP e.HFX SP e.LH;
}

void readData( string filename, vector<Item> &V );

//============================================================

int main()
{
   vector<Item> V;
   readData( "wrfdatacut.txt", V );

   cout << "All data:\n";
   for ( Item e : V ) cout << e << '\n';
  
   cout << "\nFirst column only:\n";
   for ( Item e : V ) cout << e.latitude<<"   "<< e.longitude << '\n'; 
}

//============================================================

void readData( string filename, vector<Item> &V )
{
   Item thing;
   fstream in( filename );
   while ( in >> thing ) V.push_back( thing );
   in.close();
}

//============================================================  
i want to ask how to display all of the value number i mean all of the decimal. i can't do it with cout


I think this will do the job:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
   vector<Item> V;
   readData( "wrfdatacut.txt", V );

   cout << "All data:\n";
   for ( Item e : V ) cout << e << '\n';
  
   cout << "\nFirst two columns only:\n";
   cout.setf( ios::fixed );                          // It will probably default to fixed anyway
   cout.precision( 8 );                              // If you want 8 digits after the decimal
   for ( Item e : V )
   {
       cout << setw( 14 ) << e.latitude  << "   "    // Allows for 14-character field width - should be more than enough
            << setw( 14 ) << e.longitude << '\n';
   }
}


There is a lot of information about input and output using streams in the tutorial and reference of this website.


If you don't need ALL the columns of that data (it wasn't in your original post) then you don't need to store all of them in an Item. You could simply read them into temporary local variables with the istream << operator function, rather than elements of an Item. Alternatively, if you only wanted the first two or three columns then you could read whole lines into a string and stringstream the first few into your desired elements. (This is covered in the tutorial on this website.) However, if your code is working then this is a matter for another day.
Thank you i really appreciate you help, i'm new with c++ i just know the very basic.
i even didn't know what are you writing i think i should learn at least the code you gave me. Thank you very much :)
@lastchance

can i ask another question ?
i want to save the first three columns into new array, so i can easily manipulate them. how can i do that ?
@Ryoucplus,

You already have the columns as elements of vector<Item> V; for example,
V[i].latitude
gives you the latitude of the ith row data. You can "manipulate" them pretty well exactly as you would do if you simply had an array
latitude[i]

I'm not convinced that you need to create yet more arrays repeating the same information. Can you explain what exactly you wish to do?

If you are only ever going to use the first three columns of data then you could reduce your Item structure to simply
1
2
3
4
5
6
struct Item
{
   double latitude;
   double longitude;
   double height;
};

and adjust your input operator to ignore the other items on each line.

Without knowing exactly what you ultimately want to do I am hesitant to suggest a solution that might need to be completely changed later.




i see, so this code use to call the data "V[i].latitude" i dont know it before thank you.
i want to make new data from the three column "grid point". so this "grid point" consist from 228 column and 159 rows.
i want to make for example
1
2
3
4
5
from 
63.067098	      128.98091	     0.89787897	     
63.067045	      128.98299	     0.89787812	     
63.067034	      128.98431	     0.89787834	     
63.067052	      128.98696	     0.89787853	 


1
2
3
4
5
6
7
8
9
10
to
63.067098	      128.98091	     0.89787897	     
63.067098	      128.98299	     0.89787812	     
63.067098	      128.98431	     0.89787834	     
63.067098	      128.98696	     0.89787853
63.067045	      128.98091	     0.89787897
63.067045              128.98299	 0.89787812
63.067045             128.98431	 0.89787834
63.067045            128.98696	0.89787853
and so on
@Ryoucplus

I don't quite understand you. It looks roughly as if you are trying to make a 2-d mesh of the form

lat[0] lon[0]
lat[0] lon[1]
lat[0] lon[2]
lat[0] lon[3]
...
...
lat[1] lon[0]
lat[1] lon[1]
lat[1] lon[2]
lat[1] lon[3]
...
...
lat[2] lon[0]
lat[2] lon[1]
lat[2] lon[2]
lat[2] lon[3]

etc.

However, I do not understand what exactly you are trying to do with the heights and to which of latitude or longitude they are tied.

If the mesh is equally spaced then you can create a mesh without reading any data in the first place! Also note that neither latitude nor longitude appear to be in sorted order.

Please provide a clearer statement of what you are trying to do. Is it written down somewhere?


Also, do you need the rest of the data on each line? (The PSFC, U10, V10 etc.). If this is not needed then it would be better excluded from your Item stored data right now.


Last edited on
@lastchance

Hello there, can i still asking? it seem you dont understand my last question so i want ask other question.
how we can save first three column without ignore the other item?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Item
{
   double latitude;
   double longitude;
   double height;
   double PSFC;
   double u10;
   double v10;
   double t2;
   double q2;
   double RAINC;
   double RAINNC;
   double SNOWNC;
   double GRAUPELNC;
   double SWDOWN;
   double GLW;
   double UST;
   double HFX;
   double LH;
   
};

but we just one to save first three column in a multidementional array
1
2
3
double latitude;
   double longitude;
   double height;
I'm sorry, @Ryoucpluscplus, but I really don't understand what you are trying to do. I can't attempt to give you an answer which may make the situation even more confused.
@lastchance i wonder why the situation is more confused to you rather than it confused to me, haha
i'm just glad for everything you write so i can learn from that.
thank you so far i really helped
@Ryoucplusplus, I’m not good as English, so I’m just trying to guess.
What you want is an array which stores Item.latitude, Item.longitude and Item.height from every row of std::vector V?
If so, please have a look at the following code:
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
#include <algorithm>
#include <array>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>

struct Item
{
    double latitude;
    double longitude;
    double height;
    double PSFC;
    double u10;
    // The example you posted contains only 5 columns of data
    // double v10;
    // double t2;
    // double q2;
    // double RAINC;
    // double RAINNC;
    // double SNOWNC;
    // double GRAUPELNC;
    // double SWDOWN;
    // double GLW;
    // double UST;
    // double HFX;
    // double LH;
    friend std::istream& operator>>(std::istream& strm, Item &e)
    {
        strm >> e.latitude >> e.longitude >> e.height >> e.PSFC  >> e.u10
             // >> e.v10      >> e.t2        >> e.q2     >> e.RAINC >> e.RAINNC
             // >> e.SNOWNC   >> e.GRAUPELNC >> e.SWDOWN >> e.GLW   >> e.UST
             // >> e.HFX      >> e.LH
             ;
        return strm;
    }
    friend std::ostream& operator<<(std::ostream& strm, const Item& e)
    {
        return strm << std::setw(14) << std::setprecision(8) << e.latitude  << ' '
                    << std::setw(14) << std::setprecision(8) << e.longitude << ' '
                    << std::setw(14) << std::setprecision(8) << e.height    << ' '
                    << std::setw(14) << std::setprecision(8) << e.PSFC      << ' '
                    << std::setw(14) << std::setprecision(8) << e.u10       << ' '
                    // << std::setw(14) << std::setprecision(8) << e.v10       << ' '
                    // << std::setw(14) << std::setprecision(8) << e.t2        << ' '
                    // << std::setw(14) << std::setprecision(8) << e.q2        << ' '
                    // << std::setw(14) << std::setprecision(8) << e.RAINC     << ' '
                    // << std::setw(14) << std::setprecision(8) << e.RAINNC    << ' '
                    // << std::setw(14) << std::setprecision(8) << e.SNOWNC    << ' '
                    // << std::setw(14) << std::setprecision(8) << e.GRAUPELNC << ' '
                    // << std::setw(14) << std::setprecision(8) << e.SWDOWN    << ' '
                    // << std::setw(14) << std::setprecision(8) << e.GLW       << ' '
                    // << std::setw(14) << std::setprecision(8) << e.UST       << ' '
                    // << std::setw(14) << std::setprecision(8) << e.HFX       << ' '
                    // << std::setw(14) << std::setprecision(8) << e.LH
                    ;
    }
};

void readData(const std::string& filename, std::vector<Item>& V);

int main()
{
    std::vector<Item> v;
    readData("wrfdatacut.txt", v);
    std::cout << "All data:\n";
    for(const auto& e : v) { std::cout << e << '\n'; }
    std::cout << "\nFirst two columns only:\n";
    for(const auto& e : v) { std::cout << e.latitude << '\t' << e.longitude << '\n'; }
    std::vector<std::array<double, 3>> threecols(v.size());
    for(size_t i{}; i<v.size(); ++i) {
        threecols.at(i).at(0) = v.at(i).latitude;
        threecols.at(i).at(1) = v.at(i).longitude;
        threecols.at(i).at(2) = v.at(i).height;
    }
    std::cout << "\nthreecols contains:\n";
    for(const auto& a : threecols) {
        for(const auto& b : a) {
            std::cout << std::setw(14) << std::setprecision(8) << b << ' ';
        }
        std::cout << '\n';
    }
}

void readData(const std::string& filename, std::vector<Item>& V)
{
    Item thing;
    std::ifstream in(filename);
    while(in >> thing) { V.push_back(thing); }
    in.close();
}


Output:
All data:
     63.067098      128.98691     0.89787897     0.97687987     0.67764764
     63.067045      128.98691     0.89787812     0.97687943     0.67764743
     63.067034      128.98431     0.89787834     0.97687953     0.67764744
     63.067052      128.98696     0.89787853     0.97687956     0.67764744

First two columns only:
63.067098       128.98691
63.067045       128.98691
63.067034       128.98431
63.067052       128.98696

threecols contains:
     63.067098      128.98691     0.89787897
     63.067045      128.98691     0.89787812
     63.067034      128.98431     0.89787834
     63.067052      128.98696     0.89787853


Anyway, I’d keep lastchance’s advice into higher consideration: he pointed out very good alternatives to a potentially expensive copy of values you’ve already got in memory.
Hi @Enoizat,

Do your best but I don't think this poster knows what he wants. He started with a 5-column data file.

Then he switched it to a 17-column data file.

Then he started another (few) thread(s) with alternative things in the first few columns.

Then he asked for:
"i want to make new data from the three column "grid point". so this "grid point" consist from 228 column and 159 rows."
. He then proceeded to illustrate that number of rows and columns (228 x 159 ?) with the following diagram
1       2       3
1       5       6
1       8       9
1       11      12
1       14      15
4       2       3
4       5       6
4       8       9
4       11      12
4       14      15
7       2       3
7       5       6
7       8       9
7       11      12
7       14      15
10      2       3
10      5       6
10      8       9
10      11      12
10      14      15
13      2       3
13      5       6
13      8       9
13      11      12
13      14      15


He's also got a post on Stack Overflow which is receiving about the same bemused reception.

It's a moving and impossibly confused target. If we could see the original assignment or project specification it might help - even if it has to be translated from Japanese.

If you can work out the requirements ... please write down a simple version!
Last edited on
Pages: 12