Comp Sci C++ Opening a txt file using argc and argv

  • Thread starter Thread starter Joon
  • Start date Start date
  • Tags Tags
    C++ File
AI Thread Summary
The discussion revolves around a C++ program designed to calculate the minimum distance between a reference point and multiple other points read from a text file. The user encounters issues when running the code in a debugger, suspecting it cannot access the specified text file. Solutions suggested include ensuring the file is in the same directory as the source code or providing the full path to the file in the command line argument. The user confirms that following this advice resolved the issue. The conversation also hints at a simpler alternative approach to the problem that the user is exploring.
Joon
Messages
85
Reaction score
2

Homework Statement


I have a complete code that calculates distances between point 1(x1,y1,z1) and other points(from 2nd up to more than 100) and the nth point that results in minimum distance and the minimum distance between point 1 and nth point. The code is below.

The code is 100% correct, but it doesn't work when I run on Debugger. I think it is because the code has no access to the actual txt file. How do I pass the filename or the actual file as argv?

Thank you.

Homework Equations

The Attempt at a Solution


C:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <limits>
using namespace std;

struct Point3d {
    double x, y, z;
};

ostream& operator << (ostream& os, const Point3d& p) {
    os << "(" << p.x << ", " << p.y << ", " << p.z << ")";
    return os;
}

istream& operator >> (istream& is, Point3d& p) {
    is >> p.x >> p.y >> p.z;
    return is;
}

double distance(const Point3d &a, const Point3d &b) {
    auto squared = pow(a.x - b.x, 2) +
        pow(a.y - b.y, 2) +
        pow(a.z - b.z, 2);
    return sqrt(squared);
}

int main(int argc, char **argv) {
    if (argc != 2) {
        cerr << "Exactly one argument expected, got " << argc << "\n";
        return 1;
    }
      ifstream inFile(argv[1]);
    if (!inFile.is_open()) {
        cerr << "error: failed to open '" << argv[1] << "'\n";
        return 1;
    }

    // discard the header line
    inFile.ignore(numeric_limits<streamsize>::max(), '\n');
  

    Point3d first_pt;
    if (!(inFile >> first_pt)) {  // read the first point
        cerr << "error: failed read of the first point\n";
        return 1;
    }

    bool other_points = false;
    double dist_min = numeric_limits<double>::max();
    Point3d closest, current;
    while (inFile >> current) {  // loop through the other points
        other_points = true;
        double dist = distance(first_pt, current);
        if (dist < dist_min) {
            dist_min = dist;
            closest = current;
        }
    }
    inFile.close();

    if (other_points) {
        cout << "closest point to " << first_pt <<
            " is " << closest << " [distance: " << dist_min << "]\n";
    }
    else {
        cout << "There was only one point in the file\n";
    }
    system("pause");

}
 
Last edited:
Physics news on Phys.org
Joon said:

Homework Statement


I have a complete code that calculates distances between point 1(x1,y1,z1) and other points(from 2nd up to more than 100) and the nth point that results in minimum distance and the minimum distance between point 1 and nth point. The code is below.

The code is 100% correct, but it doesn't work when I run on Debugger. I think it is because the code has no access to the actual txt file. How do I pass the filename or the actual file as argv?
Are you using Visual Studio? And are passing just the file name without its full path? If so, the debugger looks for input files in the same directory as the source code. If the file isn't there, the ifstream constructor will fail. If you run you program from a command prompt window (i.e., outside of the debugger) the file will need to be in the same directory as your executable.
As an alternative, you could pass the full path of the file as one of the arguments in the argv vector.
Joon said:
Thank you.

Homework Equations

The Attempt at a Solution


C:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <limits>
using namespace std;

struct Point3d {
    double x, y, z;
};

ostream& operator << (ostream& os, const Point3d& p) {
    os << "(" << p.x << ", " << p.y << ", " << p.z << ")";
    return os;
}

istream& operator >> (istream& is, Point3d& p) {
    is >> p.x >> p.y >> p.z;
    return is;
}

double distance(const Point3d &a, const Point3d &b) {
    auto squared = pow(a.x - b.x, 2) +
        pow(a.y - b.y, 2) +
        pow(a.z - b.z, 2);
    return sqrt(squared);
}

int main(int argc, char **argv) {
    if (argc != 2) {
        cerr << "Exactly one argument expected, got " << argc << "\n";
        return 1;
    }
     ifstream inFile(argv[1]);
    if (!inFile.is_open()) {
        cerr << "error: failed to open '" << argv[1] << "'\n";
        return 1;
    }

    // discard the header line
    inFile.ignore(numeric_limits<streamsize>::max(), '\n');
 

    Point3d first_pt;
    if (!(inFile >> first_pt)) {  // read the first point
        cerr << "error: failed read of the first point\n";
        return 1;
    }

    bool other_points = false;
    double dist_min = numeric_limits<double>::max();
    Point3d closest, current;
    while (inFile >> current) {  // loop through the other points
        other_points = true;
        double dist = distance(first_pt, current);
        if (dist < dist_min) {
            dist_min = dist;
            closest = current;
        }
    }
    inFile.close();

    if (other_points) {
        cout << "closest point to " << first_pt <<
            " is " << closest << " [distance: " << dist_min << "]\n";
    }
    else {
        cout << "There was only one point in the file\n";
    }
    system("pause");

}
 
Thank you, that's solved the problem.
I saw you've replied to my another approach to this problem, I think that one could be a lot simpler than the code written here. Could you have a look at the other one?
 
Last edited:
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
15
Views
3K
Replies
1
Views
2K
Replies
3
Views
1K
Replies
9
Views
3K
Replies
8
Views
2K
Replies
3
Views
2K
Back
Top