reading txt file

I am trying to read a txt file line by line but I don't understand why my code can not open the txt file.

mesh.h

1
2
3
4
5
6
7
8
9
10
class Mesh {
public:

	size_t nLattice_;
	VecVecDbl_t Coordinate_;

	Mesh() = default;
	void ReadMesh(std::string FileName);

};

mesh.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void Mesh::ReadMesh(std::string FileName) {
	double x;
	double y;
	double z;
	std::ifstream fin(FileName);
	if (!fin) {
		std::cout << "File is not open" << std::endl;
	}
	std::string line;
	bool ToFill = false;
	int icol;

	VecDbl_t aux_coord(3, 0);

	while (getline(fin, line)) {


		std::stringstream is(line);

		if (line == "OFF") {
			ToFill = true;
		}

		if (line == "END OFF") {
			ToFill = false;
		}

		if (ToFill == true) {

			while (is >> x >> y >> z) {
				aux_coord[0] = x;
				aux_coord[1] = y;
				aux_coord[2] = z;
				Coordinate_.push_back(aux_coord);
			}
		}
	}
}

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char* argv[])
{
	display();
	int myrank, num_procs;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

	Mesh Mesh;
	Mesh.ReadMesh("mesh.txt");
	MPI_Finalize();
	return 0;
}



the text file is something like below;

OFF
2780 5556 0
10 1 70
8.59209 1.110805 70
9.056872 1.049553 69.02785
10 1 68.6
7.218847 1.440491 70
7.814551 1.269375 68.6786
8.899679 1.067515 67.86067
10 1 67.2
END OFF


Any Idea why It's not opening the file?
Last edited on
> Any Idea why It's not opening the file?

Verify that the path (file name) is correct.
(If the file is in some other directory, provide a full path.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>

int main()
{
    const std::string file_name = "whatever" ;

    if( std::ifstream file{file_name} ) // if the file is opened for input
    {
        // read lines in the file
        // ...
    }

    else // failed to open file
    {
        std::cout << "*** error *** failed to open the file for input.\nfile_name: " << file_name
                  << "\nabsolute path: " << std::filesystem::absolute(file_name).string()
                  << "\n(working directory is: " << std::filesystem::current_path().string() << ")\n" ;
    }
}
The most likely reason is that input file is not in the run directory.

However, you have called the mesh read routine within your MPI code, which means that ALL processors will try to open and read the file and, let's just say, they aren't very good at sharing.

If you are running a multi-processor code then let root read the mesh and distribute it to other processors in an orderly fashion. If you don't want to do that yet then invoke the program (the mpirun call) with only one processor.
Last edited on
There is a logic issue. If OFF or END OFF is found, the code then tries to extract from the string stream buffer. If these are found, then the while should be continued. Something like (NOT tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool Mesh::ReadMesh(const std::string& FileName) {
	std::ifstream fin(FileName);

	if (!fin) {
		std::cout << "File is not open\n";
		return false;
	}

	bool ToFill {};

	for (std::string line; getline(fin, line); ) {
		if (line == "OFF") {
			ToFill = true;
			continue;
		}

		if (line == "END OFF") {
			ToFill = false;
			continue;
		}

		if (ToFill) {
			std::istringstream is(line);

			for (double x, y, z; is >> x >> y >> z; Coordinate_.emplace_back(x, y, z));
		}
	}

	return true;
}

Last edited on
If you're on Windows:
1. Turn OFF the "hide extensions of known file types" in file explorer.

2. Verify that the file is really called mesh.txt and not something like mesh.txt.txt if you saved the file using notepad and it automatically assumed an extension for you.

3. Make sure that your file is ASCII encoded and not Unicode.
@lastchance For the time being MPI won't make any problem, because I'm working with one processor but you are right.
Last edited on
@JLBorges Thanks for your answer. The path is correct.
@salem c it's already turned off. when I go to properties to see the file detail, in name section I see mesh.txt. When I open it with notepad, on the top, I see mesh.txt - notepad. can that be a problem?
Last edited on
@seeplus That's not a problem. If I remove that part still the code won't work.
Compile and run this code in the same directory. What does it show?

Also, state what operating system you are running on, which compiler, and which MPI platform.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include "mpi.h"
using namespace std;

int main( int argc, char* argv[] )
{
   int myrank, num_procs;
   MPI_Init( &argc, &argv );
   MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
   MPI_Comm_size( MPI_COMM_WORLD, &num_procs );

   ifstream fin( "mesh.txt" );
   cout << "Processor " << myrank << " reports file " << ( fin ? "is" : "is  not" ) << " open\n";
   MPI_Barrier( MPI_COMM_WORLD );
   MPI_Finalize();
}
Last edited on
operating system, windows 64bit. visual studio 2022. I'm using Microsoft MPI v10.0.
I run above code and the following is the result.
Processor 0 reports file is not open
On Windows, add the following line to the beginning of your program (and #include <cstdlib>):
system("cd");
This prints out the current (working) directory.
The location of mesh.txt needs to match this directory.

(Note to others reading, *nix equivalent is system("pwd");)
Last edited on
I think I would go for
system( "dir" );
You might get a few surprises.

Why don't you just run the executable from the command line?

When run with one processor mine reports:
Processor 0 reports file is open
Last edited on
Cplusc wrote:
visual studio 2022

Visual Studio is notorious for having different default run locations, depending on how you run the executable. And how you initially set up your project/solution.

Are you running the executable from a command line or using the IDE?

If running the exe from the command line your input file must reside in the same location as the executable.

If running the exe via the IDE the input file must be in the same location as your source files.
Topic archived. No new replies allowed.