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?
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';
}
//============================================================
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:
@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.
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.
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 :)
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
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
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.
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?
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:
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.
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!