0

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.

Edited by argio: n/a

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by StuXYZ
0

I can't get the code to seg fault but I am running it on linux.

However you have many instances of code like: if (wrote=false) and similar Note: you only have ONE =. This is certainly wrong. Consider the line above. It would set wrote to false and then never execute the code in the error block. PLEASE (a) get a compiler that gives warning messages, (b) FIX THEM. -- I almost missed the == error in the storm of other warning messages.

If you are still getting seg-faults can you suggest the input information you typed to get that error??

You know the basics, don't use using namespace std; , don't name stuff like System etc, that is so easy to miss-type, don't mix '\n' and std::endl; [use the latter], don't pass things by value that would be better by const reference. e.g webaddr to System::Write( ). You will have less problems if you code to these standards, it then becomes instinctive.

Edited by StuXYZ: n/a

0

I'm using Dev C++? I fixed those errors and yet it still compiles fine and instantly closes with the segmentation fault error after compiling.

0

well then the first thing to do is to find out if you get to your first system command. Put a std::cout<<"TEST"<<std::endl; exit(1) at line 17 of your main. Then see if you get that without a seg fault.

If you don't then you have an error in the constructor of System.
If not then it maybe the system command and so on. A debugger can help here lots.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.