简体   繁体   中英

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. However, I am not sure how to make the code work for more than 4 points. The text file I am trying to extract data from looks like:

Coordinates of many points (1st line)

xyz (second)

-0.06325 0.03597 0.042823 (third line)

. . . . . . . . .

continued .. and point 1 has coordinates (-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? 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) .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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