473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(string);

// 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.
Sep 24 '07 #1
3 1414
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?
Sep 24 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1. 131.123.47.176 - - [18/Sep/2002:12:05:25 -0400] "GET /~darci/ HTTP/1.0" 200 5625
  2. 198.143.205.166 - - [18/Sep/2002:12:06:06 -0400] "GET /~reichel/b.jpg HTTP/1.1" 200 16514
  3. 24.203.197.200 - - [18/Sep/2002:12:06:46 -0400] "GET /~jkuleck/bannerbar.gif HTTP/1.1" 304 -
  4. 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
Sep 24 '07 #3
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.
Sep 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
0
by: Pud | last post by:
Hiya, I was wondering if anyone could help me, I am trying to create an example program that uses the xerces XML parser in Java. Unfortunately I have come across a problem when using this...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
2
by: Ken Shaffer | last post by:
I am trying to install DB2 UDB 8.1 on linux. I get the segmentation fault on line 130 of db2jinst that others have mentioned. I tried: install on RH Fedora Core 2. got the seg fault error,...
21
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
1
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std;...
0
by: dboileau | last post by:
Can anyone help me out with this error, I am trying to compile mysql with gcc 3.4.6 (Compiled from source) using CC=gcc CFLAGS="-O3 -mcpu=v8 -Wa,-xarch=v8plusa" \ CXX=gcc CXXFLAGS="-O3...
2
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I...
1
by: ziroi | last post by:
Hello! I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.