简体   繁体   English

C++ 读取数据文件

[英]C++ Reading a data file

I have successfully opened and discarded the headers.我已经成功打开并丢弃了标题。 Then I calculated the distances between point 1 and others and the minimum distance as well.然后我计算了点 1 和其他点之间的距离以及最小距离。 However, I am not sure how to make the code work for more than 4 points.但是,我不确定如何使代码工作超过 4 点。 The text file I am trying to extract data from looks like:我试图从中提取数据的文本文件如下所示:

Coordinates of many points (1st line)多点坐标(第一行)

xyz (second) xyz(第二)

-0.06325 0.03597 0.042823 (third line) -0.06325 0.03597 0.042823(第三行)

. . . . . . . . . . . . . . . . . .

continued .. and point 1 has coordinates (-0.06325, 0.03597 , 0.042823).继续 .. 点 1 的坐标为 (-0.06325, 0.03597, 0.042823)。

Also, I need to tell which is the closest point to point 1 and the distance between point 1 and the closest point on the output screen, could you help me?另外,我需要知道哪个是最接近点 1 的点以及点 1 和输出屏幕上最近点之间的距离,你能帮我吗? Thank you.谢谢你。

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int main() {

double x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, dist_12, dist_13, 
dist_14, minimum_distance;
ifstream inFile("input-week6-ad-q4-2.txt"); // ifstream function to read the 
file
string line, c; // To read the characters
char ch;


if (inFile.is_open())
{
    getline(inFile, line); // To read the header of the input file then 
discard it
    getline(inFile, line);



    inFile >> x1 >> y1 >> z1;
    inFile >> x2 >> y2 >> z2;
    inFile >> x3 >> y3 >> z3;
    inFile >> x4 >> y4 >> z4;

    int i = 1;
    while (inFile >> xi >> yi >> zi) {

        i++;    
    }
    int number_of_points = i;

    inFile.close();

    }

  else
    cout << "The file could not be opened." << "\n"; // To check for any 
 error

system("pause");
return 0;
 }

As mentioned in the comments, a preferred way to read a file of unknown length with ifstream is to use something like while (inFile >> xi >> yi >> zi) .如评论中所述,使用ifstream读取未知长度文件的首选方法是使用类似while (inFile >> xi >> yi >> zi)的东西。

To track the closest point and distance, check this after each read and update the shortest distance and the point index if you find one that is closer than the last point.要跟踪最近的点和距离,请在每次读取后检查并更新最短距离和点索引,如果您发现比最后一个点更近的点。

Since you were already using sqrt() , I thought it was OK to use the pow() function to calculate the square in the distance formula.由于您已经在使用sqrt() ,我认为可以使用 pow() 函数来计算距离公式中的平方。

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <vector>

int main()
{
  double xFirst, yFirst, zFirst, xi, yi, zi;
  std::ifstream
      inFile("input-week6-ad-q4-2.txt");
  std::string line; // To read the characters
  double min_distance{UINT_MAX}; // Start with largest distance possible.
  uint closest_point;
  
  if (inFile.is_open()) {
    // read and discard first 2 lines of file
    getline(inFile, line); getline(inFile, line);

    // Save first point's coordinates
    inFile >> xFirst >> yFirst >> zFirst;
    int i = 1;
    while (inFile >> xi >> yi >> zi) { // safer way to read 3 vars from file
      i++;
      // use the pow(base, power) function for squaring
      double distance = sqrt(pow((xi - xFirst), 2)
                                 + pow((yi - yFirst), 2)
                                 + pow((zi - zFirst), 2));
      if (distance < min_distance) {
        min_distance = distance;
        closest_point = i;
      }
    }
    std::cout << "Read " << i  << " points\n";

    inFile.close();
  }

  std::cout << "The minimum distance is " << min_distance
            << ", which is the distance between point[1] and point["
            << closest_point << "], using 1-based indexing.\n";

  std::cin >> line;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM