如何使用c++访问文本文件中的特定值



我的文本文件有这样的结构和这些值

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

每列是不同类型的数据,第一列是weight,第二列是size等等。例如,用户请求权重,这将是第一列

15.32
14.58
74.58

,我需要打印

reg 1 reg 2 reg 3
15.32 14.58 74.58

同样,用户可以请求其他列,我不知道怎么才能做到这一点我只能打印第一行

15.32 15.00 14.58 14.36 17.85 01.95 15.36 

使用这段代码,但只有当我使用整数文件时,如果它们是double,下面的代码什么也不做

string nextToken;
while (myfile>> nextToken) {
    cout << "Token: " << nextToken << endl;
}

但是我不知道如何在列和行之间移动

我正在使用这个结构

struct measures{
    string date;
    double weight;
    double size;
    double fat;
    double imc;
    double chest;
    double waist;
} dataclient;

我读取如下的值

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<<" ";

对于一个用户可以执行多次,然后关闭文件

之后,用户请求任何值

要做到这一点,一个简单的方法是创建一个结构或类来封装出现在"记录"中的数据。(记录为一行)将每一行读入该类的新实例,然后从需要的适当成员变量中提取数据。

编辑:另外,我想补充的是,这个答案给了我一些1337代表:)

更简单的方法是用两个参数创建一个函数:起始项和"stride",即每行的项数。然后,您可以跳过项目,直到开始项目,然后跳过连续项目之间的跨步:

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;
  }
}

对于您的具体示例,您可以将其称为printcolumn(0, 7, myfile);

看看这个MSDN论坛,Visual c++ General对于具有两列的文件,有一种解决方案,但它可以是任意数量的列。在std::string中读取文本,然后在std::vector中写入文本,然后处理其元素。

这段代码将measure s的整个文件加载到一个名为data的向量中,并记住它的所有内容。然后,当用户想要访问特定的measure时,您可以简单地从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";
}

相关内容

  • 没有找到相关文章

最新更新