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

时间:2011-09-01 17:52:36

标签: c++ stream console token fstream

我的文本文件具有此结构和此值

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

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

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 

使用此代码,但仅当我使用整数文件时,如果它们是双倍,则以下代码不执行任何操作

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

这可以为用户多次完成,然后关闭文件

之后,用户请求任何值

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

此代码将measure的整个文件加载到名为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";
}

答案 3 :(得分:0)

看一下这个帖子MSDN Forum, Visual C++ General 有一个包含两列的文件的解决方案,但它可以是任意数量的列。 您在std :: string中读取文本,然后将其写入std :: vector,然后使用其元素。