C++ Opening a txt file using argc and argv

  • Comp Sci
  • Thread starter Joon
  • Start date
  • Tags
    C++ File
In summary, the conversation is about a code that calculates distances between a first point and other points, and finding the closest point to the first point. The code is correct but does not work when run on a debugger, possibly due to the file not being accessible. The solution is to either pass the full file path as an argument or run the program outside of the debugger.
  • #1
Joon
85
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
  • #2
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");

}
 
  • #3
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:

1. How do I open a txt file using argc and argv in C++?

The first step is to include the fstream library in your code. Then, you can use the ifstream function to open the file. The file name will be provided as a command line argument, so you can use argv[1] to access it. For example: ifstream file(argv[1]);

2. How do I check if the file was successfully opened?

After using the ifstream function to open the file, you can use the is_open() function to check if the file was successfully opened. This function returns a boolean value, so you can use an if statement to handle any errors.

3. How do I read data from the txt file?

You can use the getline() function to read a line of data from the file. This function takes in two parameters - the file name and a string variable to store the data in. You can use a while loop to read all the lines of data from the file.

4. How do I close the txt file after reading the data?

After you have finished reading the data from the file, you can use the close() function to close the file. This function takes no parameters and will close the file that was opened using the ifstream function.

5. Can I open multiple txt files using argc and argv?

Yes, you can open multiple txt files using argc and argv. You can use argv[2], argv[3], and so on to access the file names provided as additional command line arguments. Just make sure to open and close each file separately.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Programming and Computer Science
Replies
6
Views
931
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top