Like Tree1Likes
  • 1 Post By Salem

Need help fixing segmentation fault

This is a discussion on Need help fixing segmentation fault within the C++ Programming forums, part of the General Programming Boards category; Hi, I was just wondering if anyone could help me fix the segmentation fraud in the programme below: Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Need help fixing segmentation fault

    Hi, I was just wondering if anyone could help me fix the segmentation fraud in the programme below:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    void Parse(string, string&, string&, string&, double&, int&);
    void Display(string, string, string, double, int);
    
    int main()
    {
    	fstream InFile("WindStation00.data", ios::in);
    	
        string Line, ID, Date, Hour;
    	double Speed;
    	int Direction;
    
    	while(!InFile.eof())  {
    	    InFile >> Line;
    		cout << "LINE: " << Line << endl;
    	    Parse(Line, ID, Date, Hour, Speed, Direction);
    		Display(ID, Date, Hour, Speed, Direction);
    
    	}
        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 Display(string ID, string Date, string Hour, double Speed, int Direction)
    {
    	     cout << "\tID = " << ID << endl;
    		cout << "\tDate = " << Date << endl;
    		cout << "\tHour = " << Hour << endl;
    		cout << "\tSpeed = " << Speed << endl;
    		cout << "\tDirection = " << Direction << endl;
    }
    I will greatly appreciate any suggestions. Thank You.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Code:
            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...
    Yes, you go and lookup stringstream and find out how to break up a std::string using C++.

    What you've done on line 1 is taken a very large axe, smashed down the door of data encapsulation and proceeded to trash all the internal data of a string object with your use of strtok()

    > while(!InFile.eof())
    And read the FAQ on why eof is a bad way of controlling a loop.
    antred likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    First off, it's a segmentation FAULT. Secondly, do yourself a favor and run your program in a debugger. That way, you won't have to guess where the segmentation fault occurs ... your debugger will show you.

    That said, I'm guessing the problem is somewhere in your Parse() function. Messing around with those old C-style functions is asking for trouble. Don't use strtok() on an std::string object. You're corrupting the object's internal state.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By DeanWinchester in forum C Programming
    Replies: 10
    Last Post: 12-28-2011, 06:48 AM
  2. Segmentation Fault -_-
    By Bro in forum C Programming
    Replies: 30
    Last Post: 12-27-2011, 03:43 AM
  3. Segmentation Fault?
    By Chinnie15 in forum C Programming
    Replies: 5
    Last Post: 11-17-2011, 01:23 PM
  4. Need help fixing segmentation fault
    By Robbie in forum C Programming
    Replies: 46
    Last Post: 09-11-2011, 07:41 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21