SEGMENTATION FRAUD

Hi, I have been working on this programme for 3 hours now. And, whenever I try to compile the programme, it says the programme has segmentation fraud. I tried my best to fix the problem but I could not fix it. So, here I am asking for help to fix this problem. Below is the code for the programme

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//Define a class to represent each wind measurement event...

class WindStationData  {
  string LocationID;  	//This is the station that owns this piece of data…
  string Date;  		 //The date that the values were collected…
  string Time;  		//The time of day that the values were collected…
  double Velocity; 	//The wind speed…
  int Direction;  	//The direction of the wind…

public:
  WindStationData();
  WindStationData(string, string, string, double, int);

  //The first group of member functions are used to change values...
  //These are so simple, it makes sense to define as inline...

  void SetID(string ID)					{ LocationID = ID; }
  void SetDate(string Day)			{ Date = Day; }
  void SetTime(string Hour)			{ Time = Hour; }
  void SetVelocity(double Speed)	{Velocity = Speed; }
  void SetDirection(int Degrees)	{ Direction = Degrees; }

  //The second group allows controlled access to individual value in the class...

  string GetID()				{ return LocationID; }
  string GetDate()			{return Date; }
  string GetTime()			{return Time; }
  double GetVelocity()	{ return Velocity;}  
  int GetDirection()	{ return Direction; }

};

//The default constructor sets the private variable members to a harmless value...

WindStationData::WindStationData()
{
	LocationID = "";
	Date = "";
	Time = "";
	Velocity = 0.0;
	Direction = 0;
}

//This initializing  constructor allows you to create a object with value already set...

 WindStationData::WindStationData(string ID, string Day, string Hour, double Speed, int Degrees)
{
	LocationID = ID;
	Date = Day;
	Time = Hour;
	Velocity = Speed;
	Direction = Degrees;
}
 
 //Declare useful functions...

void Parse(string, string&, string&, string&, double&, int&);
void AddData(WindStationData*, string, string, string, double, int);
void Display(WindStationData*, int MaxSize);

const int Size = 24;

int main()
{  
	WindStationData List[24];

	fstream InFile("WindStation00.data", ios::in);
	  
	string Line, ID, Date, Hour;
	double Speed;
	int Direction;

	while(!InFile.eof())  {
	    InFile >> Line;
		if(!InFile.eof())  {
			cout << "LINE: " << Line << endl;
			Parse(Line, ID, Date, Hour, Speed, Direction);
			AddData(List, ID, Date, Hour, Speed, Direction);
		}
	}

	Display(List, Size);
    InFile.close();

	system("pause");
}
	
void Parse(string Line, string& ID, string& Date, string& Hour, double& Speed, int& Direction)
{	 
		ID = strtok((char*)Line.c_str(), ":") ;
		Date =  strtok(0, ":");
		Hour = strtok(0, ":");
		
		Speed = atof(strtok(0, ":") ); //Converts a string of digits to a double...
        Direction = atoi( strtok(0, ":") ); //Converts a string of digits to an int...	
}

void AddData(WindStationData* List, string ID, string Date, string Hour,	double Speed, int Degrees)
{
	int Index = atoi(Hour.c_str());

	List[Index].SetID(ID);
	List[Index].SetDate(Date);
	List[Index].SetTime(Hour);
	List[Index].SetVelocity(Speed);
	List[Index].SetDirection(Degrees);
}

void Display(WindStationData* List, int MaxSize)
{
	int K;

	for(K = 0 ; K < MaxSize ; K++)  {
		cout << "==================================" << endl;
		cout << "\tID = " << List[K].GetID()  << endl;
		cout << "\tDate = " << List[K].GetDate() << endl;
		cout << "\tHour = " << List[K].GetTime() << endl;
		cout << "\tSpeed = " << List[K].GetVelocity() << endl;
		cout << "\tDirection = " << List[K].GetDirection() << endl;
		cout << "==================================" << endl;
	}
}


Any suggestions or help will be greatly appreciated. Thank You.
You mean "Segmentation fault". That can happen when your program tries to access memory it shouldn't.

Make sure that strtok doesn't return NULL before you assign it to the strings.

Make sure Index is in the range 0-23.

You could also use a memory debugger like valgrind to find out where the segfault happens.

EDIT:
Note that InFile >> Line; will just read one word. Use std::getline to read whole lines.
Last edited on
If the text file fails to open, your code has an empty Line variable but still attempts to parse the data, leading to a seg fault.

Add a check that the file is opened correctly, and change how your while loop works - the flag for EOF will only be set after attempting to read, so in your current code, you will always read an empty line and then try to parse that empty line, leading to a seg fault.
Last edited on
Topic archived. No new replies allowed.