my text file has this structure and this values
15.32 15.00 14.58 14.36 17.85 01.95 15.36
14.58 21.63 25.00 47.11 48.95 45.63 12.00
74.58 52.66 45.55 47.65 15.55 00.23 78.69
each column is a different type of data, the first column is weigh , second is size and so on. the user requests for instance the weight, which would be the first column
15.32
14.58
74.58
and i need to print
reg 1 reg 2 reg 3
15.32 14.58 74.58
also, the user can request other column i don't know how i can accomplish this i am able only to print the first line
15.32 15.00 14.58 14.36 17.85 01.95 15.36
using this code, but only if i use integer files, if they are double the below code does nothing
string nextToken;
while (myfile>> nextToken) {
cout << "Token: " << nextToken << endl;
}
but i don't know how to move between columns and lines
I am using this structure
struct measures{
string date;
double weight;
double size;
double fat;
double imc;
double chest;
double waist;
} dataclient;
i read the values like this
ofstream file;
file.open (dataclient.id, ios::out | ios::app);
if (file.is_open())
{
cout<<" ENTER THE WEIGH"<<endl;
cin>>dataclient.weigh;
file<<dataclient.weigh<<" ";
cout<<" ENTER THE SIZE"<<endl;
cin>>dataclient.size;
file<<dataclient.size<<" ";
cout<<" ENTER % FAT"<<endl;
cin>>dataclient.fat;
file<<dataclient.fat<<" ";
this can be done several times for an user,and then closes the file
after that, the user request any of the value
An easy way to do things like this is create a structure or class to encapsulate the data that appears in a "record." (A record being a row) Read each row into a new instance of that class and then just pull the data from the appropriate members variables that you need.
EDIT: Also, I would like to add that this answer gave me some 1337 rep :)
Take a look at this thread MSDN Forum, Visual C++ General There is a solution for a file with two columns, but it can be any number of columns. You read text in std::string then write it in std::vector and then work with its elements.
This code loads in the entire file of
measure
s into a vector calleddata
and remembers all of it. Then when a user wants to access a particularmeasure
, you can simply read it fromdata
.An even easier way is to make a function with two parameters: the starting item and the "stride", or the number of items per row. You can then skip items until the starting item and then skip the stride between successive items:
For your specific example, you'd call it like
printcolumn(0, 7, myfile);