Segmentation Fault.

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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include "matrix_12.h"

using namespace std;

int main(int argc, char *argv[])
{
	// Command line argument goes like:   out.txt in.txt
	std::string file_in, file_out;
	file_in = argv[1];
	file_out= argv[2]; // Parameter File for Reading the Inputs
	std::vector<matrix> traces;

	if (argc !=3) {	std::cout << "Please enter correct number of arguments.. Program exiting\n" << std::endl; return 1;	}
	else std::cout << "Correct Number of arguments. Running the program" << std::endl;

	std::ifstream file_par(file_in.c_str());
	std::ofstream fileout;
fileout.open(file_out.c_str(), std::ios_base::app);


	long line_No=0;
	std::cout << "Files read, starting the Code" << std::endl;
	std::cout << "Input file name is " << file_in << std::endl;
	std::cout << "Output file name is " << file_out << std::endl;



	if (file_par.is_open())
	{
		while(!file_par.eof() )
		{
			std::string line,temp1,temp2,temp3,temp4;
			std::stringstream linestring;
			matrix temp;
			linestring.clear();
			getline(file_par,line);
			linestring <<line;
			linestring >> temp1  >> temp.tag >> temp3 >> temp4 >> temp(0,0) >> temp(0,1) >> temp(0,2) ;

			if (temp3 == "Rotation")
			{
				linestring.clear();
				linestring <<line;
				getline(file_par,line);
				linestring <<line;
				linestring >> temp1  >> temp2 >> temp(1,0) >> temp(1,1) >> temp(1,2) ;
				getline(file_par,line);
				linestring <<line;
				linestring >> temp1  >> temp2 >> temp(2,0) >> temp(2,1) >> temp(2,2) ;
				temp.computeAngle();
				temp.print(fileout);
				temp.print (std::cout);
				line_No+=2;
			}
			line_No++;
			if (line_No > 10000) break;

		}
	}
	fileout.close();
	file_par.close();

	return 0;
}


Matrix_12.h
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
63
#ifndef matrix_HH
#define matrix_HH

#include <iostream>
#include <math.h>
#define PI 3.14159265


/** Provides interface to store each face which consists of the Triangle Array (3x3)
Provides interface to Print the Triangle in particular Format
*/

class matrix {

public:
	double trace, angle;
	long tag;
	double triangle[3][3];
	matrix()
	{
		for( unsigned i = 0; i < 3; ++i )
		{
			trace = 0.0;
			tag=0.0;
			for (unsigned j=0; j<3 ; ++j)
				triangle[i][j]=0.0;
		}
	}

	inline std::ostream& print(std::ostream &os)
	{
		os << 0 << "  "<< tag << "   Angle  " << angle << "\n";
		return os;
	}

	inline  void computeAngle() 
	{
		trace = 0.0;
		for( unsigned i = 0; i < 3; ++i )
		{
			trace = trace + triangle[i][i];
		}
		
		angle = acos ((trace-1.0)/2.0) * 180.0 / PI;
	}


	inline double& operator ()(int x,int y) // i is Row Number , j is Column number
	{ 		
		return triangle[x][y];
	}
};










#endif //matrix_HH 


Hello I am new to c++.I tried to run this program in g++ Linux.The error i am getting is segmentation fault.
Check count of parameters before using them.
It leads to segfault if I don't provide any parameters

BTW:
1) <math.h> is deprecated. Use <cmath> instead
2) std::ifstream file_par(file_in.c_str());: stream constructors are accepting std::string as parameter, so use just std::ifstream file_par(file_in); (Or std::ifstream file_par(argv[1]); to avoid unnecessary assigment)
3) eof bit is set when we already tried and failed to read the file. Its use leads to errors almost everytime. Use while(std::getline(file_par, temp))
4) functions defined inside class definition are already inline. There is no need to use that keyword (actualy there is rarely is need to do so)
5) Why do you need to define operator() if triangle array is public?
Last edited on
Topic archived. No new replies allowed.