Compiler Error: Access violation (Segmentation fault) raised in your program.
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.
System.hpp
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
|
/*
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); //Initilization (with specific drive)
bool Set(int type); //Write to host w/o Manual adding (presets)
bool Write(vector <string> webaddr);
bool CurrentHostsFile();
};
|
System.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 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#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;
}
|
main.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 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/*
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");
}
|
I'm sure this doesn't perfectly follow OOP but I will fix that later. Help please!
I tried debugging it and I can't find where it errors. >.< Sorry for posting all my code but.
Last edited on
Topic archived. No new replies allowed.