开发者

How to access a specific value in a text file using C++

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<<"   ENTE开发者_如何学JAVAR 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 :)


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:

void printcolumn(int start, int stride, ifstream &in)
{
  string nextToken;

  // skip until the start
  while(start-->0) in >> nextToken;

  // and then start writing the items
  while (in >> nextToken) 
  {
    cout << "Token: " << nextToken << endl;

    // and skip the stride
    for(int i=1;i<stride;++i) in >> nextToken;
  }
}

For your specific example, you'd call it like printcolumn(0, 7, myfile);


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 measures into a vector called data and remembers all of it. Then when a user wants to access a particular measure, you can simply read it from data.

// This demo will only handle three records.
// if you want to work with more records, change this number
const unsigned int numRecords = 3;
// This defines a structure called measures, as you detailed
// I don't create a measure object, I simply tell the computer what it is.
struct measures{ 
    string date; 
    double weight; 
    double size; 
    double fat; 
    double imc; 
    double chest; 
    double waist; 
};
// This is complicated
// Basically, it makes it really easy to load a measure from a stream.
//   like a file stream or std::cin.
// I refer to this as "operator>>"
istream& operator>>(istream& i, measures& m) {
    // The function takes a stream and a measure
    //   and reads each member from the stream one by one into the measure 
    i >> m.date;
    i >> m.weight;
    i >> m.size;
    i >> m.fat;
    i >> m.imc;
    i >> m.chest;
    i >> m.waist;
    // The "operator>>()" function must always return the stream
    return i;
}

int main() {
  // A vector is a container, that holds objects
  // A vector<measures> is a container that contains measure objects.
  // I make a vector<measures> named "data".
  // I use the constructor that takes the size of the vector.
  // We want 3 measures, so I give it the number recNum(3).
  // This makes a container of recNum(3) measures.
  vector<measures> data(numRecords );
  // I assume you already know how to open a file
  std::ifstream myfile("myfile.txt");
  // Now we want to go through the file and load measures
  // recNum will be the line we're at
  // we go from 0 to numRecords(3), one at a time
  for(int recNum=0; recNum<numRecords; recNum += 1) {
      // First we get the measure to be loaded from the container
      // Since the container "owns" the object, I have to get the
      //   object by reference.  That's the "&" symbol.
      // It means I'm changing it, but I don't own it.
      // It belongs to the container.
      // We use the [recNum] to tell it which measure we want
      measures& newMeasure = data[recNum];
      // Now that we have the Measure that needs to be loaded,
      //   we call the special "operator>>" function that I wrote above
      // Yeah.   It's like magic or something.          
      myfile >> data[recNum];
  }
  // Done loading the measures into the container
  // The container now contains numRecords(3) measures.

  // Figure out the record that the user wants here.
  //   and put it in recordNum. (Remember that 0 is the first item,
  //   so 1 is the second item)
  int recordNum = 1; 

  // Again, we get the measure to be loaded from the container
  // We use the [recNum] to tell it which measure we want
  // Since the container "owns" the object, We have to get the
  //   object by reference again.  We don't "own" the object
  measures& userMeasure = data[recordNum];
  // We can access the weight of this measure with userMeasure.weight
  cout << "Record number " <<  recordNum;
  cout << " has a weight of: " << userMeasure .weight << ".\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜