C++ Opening a txt file using argc and argv

  • Context: Comp Sci 
  • Thread starter Thread starter Joon
  • Start date Start date
  • Tags Tags
    C++ File
Click For Summary
SUMMARY

The discussion focuses on a C++ program that calculates the minimum distance between a reference point and multiple points read from a text file. The issue arises when the program fails to access the text file during debugging, which is attributed to incorrect file path handling. The solution involves ensuring the text file is located in the same directory as the executable or providing the full path in the command line argument.

PREREQUISITES
  • Understanding of C++ programming, specifically file I/O using ifstream
  • Familiarity with command line arguments in C++ (argc and argv)
  • Knowledge of basic 3D geometry and distance calculations
  • Experience with debugging in Visual Studio or similar IDEs
NEXT STEPS
  • Learn about C++ file handling and error checking with ifstream
  • Explore command line argument parsing in C++ for more complex applications
  • Investigate debugging techniques in Visual Studio for effective troubleshooting
  • Study alternative data structures for storing and processing 3D points in C++
USEFUL FOR

C++ developers, students working on geometry-related projects, and anyone troubleshooting file I/O issues in C++ applications.

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) {
        count << "closest point to " << first_pt <<
            " is " << closest << " [distance: " << dist_min << "]\n";
    }
    else {
        count << "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) {
        count << "closest point to " << first_pt <<
            " is " << closest << " [distance: " << dist_min << "]\n";
    }
    else {
        count << "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:

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K