Access violation (Segmentation fault) raised in your program.
I received this error when I ran my program after compiling it. I can't find the error in my code.
main.cpp
/*
Main.cpp source file. Main module.
Copyright 2009 (GNU CC Licence) Madcow Deity Productions
*/
#include <cstdlib>
#include <iostream>
#include "System.hpp"
using namespace std;
System __main('C');
int RuleDialog();
int main(int argc, char *argv[])
{
//Init
system("title Hosts Organizer 1.0.0 by Madcow Deity Productions");
bool exit = false;
//PRG
cout << "Welcome to Hosts Organizer by Madcow Deity Productions\n ";
system("pause");
system("cls");
//Main loop
while(exit = false)
{
int in;
system("cls");
cout << "Main Menu\n______________________\n\n\n";
cout << " [1] See current hosts file\n";
cout << " [2] Set rules\n";
cout << " [3] Close\n\n";
cin >> in;
switch(in)
{
case 1:
__main.CurrentHostsFile();
break;
case 2:
__main.Set(RuleDialog());
break;
case 3:
exit = true;
break;
default:
cout << "Error" << endl;
break;
}
}
return EXIT_SUCCESS;
}
int RuleDialog()
{
system("cls");
int in;
cout << "Set rules\n_____________________\n\n\n";
cout << " [1] Reset\n";
cout << " [2] Light - includes: Facebook, Twitter, Myspace\n";
cout << " [3] Full - includes: Social Networking sites and Photosharing sites\n\n";
cin >> in;
if(in == 1 || in == 2 || in == 3)
return in;
else
cout << "ERROR WITH INPUT" << endl;
system("pause");
}
System.cpp
#include "System.hpp"
//Constructors
System::System(char drive_in)
{
drive = drive_in;
}
bool System::Set(int type)
{
system("cls");
/*
Type 2
Includes: Facebook and Myspace + TWITTER
IPs:
myspace.com 127.0.0.1
facebook.com 127.0.0.1
twitter.com 127.0.0.1
*/
/*
Type 3:
Includes: Facebook, Myspace, Twitter, Hi5, Flickr, PhotoBucket.
IPs:
www.facebook.com 127.0.0.1
www.myspace.com 127.0.0.1
www.twitter.com 127.0.0.1
www.hi5.com 127.0.0.1
www.flickr.com 127.0.0.1
www.photobucket.com 127.0.0.1
*/
//Type Write
vector <string> webaddr;
bool wrote = false;
//Reset
string reset = "localhost 127.0.0.1\n";
switch( type )
{
case Light:
{
system("cls");
//Webaddr set
webaddr.push_back("www.facebook.com 127.0.0.1\n");
webaddr.push_back("www.myspace.com 127.0.0.1\n");
webaddr.push_back("www.twitter.com 127.0.0.1\n");
//Display
std::cout << "Modifying hosts file..\n";
wrote = Write(webaddr);
if(wrote = false)
{
std::cout << "Error writing to hosts file.\nAn other program may be using it.\n";
system("pause");
return false;
}
else
{
std::cout << "Wrote to host file correctly\n";
system("pause");
}
return true;
}
break;
case Full:
{
//Webaddr set
webaddr.push_back("www.facebook.com 127.0.0.1\n");
webaddr.push_back("www.myspace.com 127.0.0.1\n");
webaddr.push_back("www.twitter.com 127.0.0.1\n");
webaddr.push_back("www.hi5.com 127.0.0.1\n");
webaddr.push_back("www.flickr.com 127.0.0.1\n");
webaddr.push_back("www.photobucket.com 127.0.0.1\n");
//Display
std::cout << "Modifying hosts file..\n";
//Stuff
wrote = Write(webaddr);
if(wrote = false)
{
std::cout << "Error writing to hosts file.\nAn other program may be using it.\n";
system("pause");
return false;
}
else
{
std::cout << "Wrote to host file correctly\n";
system("pause");
}
return true;
}
break;
case Reset:
{
ofstream myfile (drive + ":\\WINDOWS\\system32\\drivers\\etc\\hosts");
if (myfile.is_open())
{
myfile << reset;
myfile.close();
}
else std::cout << "Unable to open file";
}
break;
}
return true;
}
bool System::Write(vector <string> webaddr)
{
//Prepare to write.
ofstream myfile (drive + ":\\WINDOWS\\system32\\drivers\\etc\\hosts", ios::out);
//Write
if (myfile.is_open())
{
for(int i = 0; i < webaddr.size(); i++)
{
myfile << webaddr[i] << "\n";
}
myfile.close();
}
else return false;
return true;
}
bool System::CurrentHostsFile()
{
system("cls");
std::cout << " Hosts file\n";
std::cout << "________________________________________________\n\n\n";
//Read
string line;
ifstream myfile (drive + ":\\WINDOWS\\system32\\drivers\\etc\\hosts", ios::in);
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
std::cout << line << std::endl;
}
system("pause");
}
else return false;
return true;
}
System.hpp
/*
System class
*/
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
enum eWriteType
{
Reset,
Light,
Full
};
class System
{
private:
char drive;
public:
System(char drive_in); //Initilization (with specific drive)
bool Set(int type); //Write to host w/o Manual adding (presets)
bool Write(vector <string> webaddr);
bool CurrentHostsFile();
};
I know its not perfectly correct but I'll fix it later so don't flame me. Just help me with the compilation error.