im getting a segmentation error can anyone help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    im getting a segmentation error can anyone help

    I am writting a code that opens a file and take input a line at a time. It would store that line of info in a string and then I want to split that string up into little strings and store those strings in a vector. This is what I have so far but im getting an error can anyone help me with this.

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;


    //class Log_Entry
    //{
    // public:
    // Log_Entry(strin g);

    // string get_host() const { return _host; }

    // private:
    // string _host;

    //};



    int main ()
    {


    string line;
    ifstream myfile ("example.txt") ;
    if (myfile.is_open ())
    {
    while (! myfile.eof() )
    {
    getline (myfile,line);
    int i = 0;
    string s;
    int index = 0;
    while(line[i] != '')
    {
    s[index] = line[i];
    i++;
    index++;
    }

    cout << s << endl;
    }
    myfile.close();
    }

    else cout << "Unable to open file";

    return 0;
    }



    im then going to return the vector entries into a class.
  • Benny the Guard
    New Member
    • Jun 2007
    • 92

    #2
    I believe its your "while(line[i] != '')" loop. It appears you are trying to copy string line to string s. If so why not just copy the string directly? Either way your loop is setup wrong.

    1) You go past the end because the while condition is always true. Better to use a for loop with the size of the string.

    2) The operator [] on strings is a const reference so assignment to is not valid.

    Again though what are yu actually trying to do here?

    Comment

    • gdarian216
      New Member
      • Oct 2006
      • 57

      #3
      okay what I was tring to do is get a line of a file that has a numbers and spaces in it

      sample file line:
      Code:
      131.123.47.176 - - [18/Sep/2002:12:05:25 -0400] "GET /~darci/ HTTP/1.0" 200 5625
      198.143.205.166 - - [18/Sep/2002:12:06:06 -0400] "GET /~reichel/b.jpg HTTP/1.1" 200 16514
      24.203.197.200 - - [18/Sep/2002:12:06:46 -0400] "GET /~jkuleck/bannerbar.gif HTTP/1.1" 304 -
      4.19.70.66 - - [18/Sep/2002:12:07:34 -0400] "GET /~ydrabu/ HTTP/1.0" 200 483
      I then wanted to split the string into separate strings when I encounter a space. So I should get about 10 little strings. I was trying to make a loop that would split the first string at a space load that in a tmp string and I was then going to load that string in a vector. but I was trying to get the string to split first

      Comment

      • Benny the Guard
        New Member
        • Jun 2007
        • 92

        #4
        Ok. I get what you are doing. You can use some members of the string class to help you out. Without giving the exact code away take a look at the following members:

        1. find_first_of

        2. substr

        Should be able to create a loop that uses find_first_of witht he current position in the string (init to 0 frot he begining) which gets you the offset to the first space. Then you use the start position and (offset - 1) in substr to create your temp string. Then push this back onto the vector.

        Comment

        Working...