getline is not working

I have a class named Mesh like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _MESH_H_
#define _MESH_H_

#include "includes.h"
#include "typedefs.h"


class Mesh {
public:
	size_t Nnodes_;               
	size_t Nelms_;                   
	VecVecDbl_t Coordinates_;        
	VecVecIdx_t Connectivities_;     

	Mesh()=default;
	Mesh(const std::string &FileName);
	void Mesh_build(size_t Nnodes, size_t Nelms, VecVecDbl_t Coordinates, VecVecIdx_t Connectivities);
};

#endif 


and this is my 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Mesh.h"
#include "includes.h"
#include "typedefs.h"

Mesh::Mesh(const std::string &FileName){

	std::ifstream File(FileName);
	std::string line;
	double value;
	bool ToFill = false;

	// Read mesh file
	int icol;
	int i = 0;

	VecDbl_t aux_coord(2, 0);

	while (getline(File, line)) {
		icol = 0;
		std::stringstream iss(line);
		if (line == "Coordinates") { //start filling vectors
			ToFill = true;
		}
		if (line == "End Coordinates") { //end filling vectors
			ToFill = false;
		}
		if (ToFill == true) {
			while (iss >> value) {
				if (icol == 1) {
					aux_coord[0] = value; //x coordinates	
				}
				if (icol == 2) {
					aux_coord[1] = value; //y coordinates
				}
				Coordinates_.push_back(aux_coord);
				icol++;
			}
			i++;

		//}
		}	

	}
	Nnodes_ = Coordinates_.size();
	//Nelms_  = Connectivities_.size();

	for (int i = 0; i < Nnodes_; i++) {
		for (int j = 0; j < Coordinates_[i].size(); j++) {
			std::cout << Coordinates_[i][j] << "\n";

		}
		std::cout << std::endl;
	}

}

void Mesh::Mesh_build(size_t Nnodes, size_t Nelms, VecVecDbl_t Coordinates, VecVecIdx_t Connectivities) {
	Nnodes_ = Nnodes;
	Nelms_ = Nelms;
	Coordinates_ = Coordinates;
	Connectivities_ = Connectivities;
}

Now in the main I have this

1
2
3
4
5
6
int main()
{
	//Mesh Global_Mesh;
	Mesh Global_Mesh("Mesh");

}


but it doesn't print the result for me. thanks in advance
Last edited on
Firstly, you should check that the file has been opened OK.

Isn't there an issue with icol? What happens when it becomes > 2? What when it's 0?

What is the format of the file data?
The file data is a text file.
Actually,icol is the number of columns,if the icon is 0,it refers to the node ID,1 for x coordinate, 2 for y coordinate and 3 for z. You think the problem is icol?
and I add this line to the mesh cpp

1
2
3
4
5
6
7
8
9
10
11
12
	std::ifstream File("FileName");
	if (File.is_open()) {

		char c = File.get();
		while (File.good()) {
			std::cout << c;
			c = File.get();
		}
	}
	else {
		std::cout << "Error opening file";
	}


it says error opening.i don't know why
Last edited on
To read a file char by char:

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#include <iostream>

int main() {
	std::ifstream File("FileName");

	if (File.is_open())
		for (char c {}; File.get(c); std::cout << c);
	else
		std::cout << "Error opening file";
}


If it says there's an error opening the file, then it's probably because the file can't be found in the current directory. Does the really file exist? In what directory? Do you mean the string "FileName" or the variable FileName?

Re icol. Maybe/maybe not. At first look the code didn't seem right - but that why I asked for the format of the file data. aux_coord is being pushed for every value read from the line - whether it's values have been changed or not - which again doesn't seem right.....
Last edited on
I have a text file and the name is mesh.txt.
I want to read this file.
In another scenario I want to change the name of argument ind the program read another text file for me. The format of file data is double. At the beginning I told double value
So use "mesh.txt" instead of "FileName"

Please post the data file (or a sample if large). Just saying the format of the file data is double is not useful without knowing the layout of the file.
1
2
3
4
5
6
7
8
Coordinates
    1               4               3               0
    2               4            2.75               0
    3            3.75               3               0
    4      3.76773151      2.65921885               0
    5      3.59192947      2.79192947               0
    6               4             2.5               0
End Coordinates


@seeplus is there any problem with content of file?
Last edited on
As a function to read the mesh file and return a vector of co-ordinates, then possibly:

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
39
40
41
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <sstream>

using VecDBL_t = std::pair<double, double>;
using Coordinates_ = std::vector<VecDBL_t>;

Coordinates_ read(const std::string& FileName) {
	std::ifstream File(FileName);

	if (!File)
		return (std::cout << "Cannot open file\n"), Coordinates_{};

	bool ToFill {};
	Coordinates_ coords;

	for (std::string line; std::getline(File, line); ) {
		if (line == "Coordinates")	//start filling vectors
			ToFill = true;
		else if (line == "End Coordinates")	//end filling vectors
			ToFill = false;
		else if (ToFill) {
			std::istringstream iss(line);

			for (double r {}, x {}, y {}; iss >> r >> x >> y; coords.emplace_back(x, y));
		}
	}

	return coords;
}

int main()
{
	const auto coords {read("mesh.txt")};

	for (const auto& [x, y] : coords)
		std::cout << x << "\t\t" << y << '\n';
}



4               3
4               2.75
3.75            3
3.76773         2.65922
3.59193         2.79193
4               2.5

Last edited on
Thanks for your answer
Topic archived. No new replies allowed.