【问题标题】:How to access a specific value in a text file using C++如何使用 C++ 访问文本文件中的特定值
【发布时间】:2011-11-08 14:48:00
【问题描述】:

我的文本文件有这个结构和这个值

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 

使用此代码,但仅当我使用整数文件时,如果它们是双倍的,则下面的代码什么也不做

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

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

之后,用户请求任何值

【问题讨论】:

    标签: c++ stream console token fstream


    【解决方案1】:

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

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

    【讨论】:

    • 结构测量{字符串日期;双倍重量;双倍尺寸;双脂肪;双imc;双胸;双腰; } 医疗客户;
    • @user59594 是的,完全正确!将所有数据读入这些成员后,只需读取变量即可!
    【解决方案2】:

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

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

    【讨论】:

    • 这就像一个魅力,非常感谢,没有你的帮助我无法解决这个问题。
    【解决方案3】:

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

    【讨论】:

    • 我的C++知识几乎一无所有。我是初学者。
    • 您的代码很好(在我看来),除了一件事,您必须知道记录的数量。为什么不将每条记录读入一个临时变量,然后将其 push_back 到向量 (while (myfile &gt;&gt; tmp) data.push_back(tmp);) 中?
    • @Christian Ammer:一步一步。我考虑过,但决定不这样做。
    • @user59594:我在代码中添加了大量的 cmets,它解释了除了如何打开文件之外的所有内容。 (你显然已经可以这样做了)
    • 对不起,好奇,但你为什么决定反对呢?这样做没有任何缺点,但是如果将向量设置为正好包含 3 个元素,那么您将不知道例如该程序只读取了 2 条记录。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多