Unable to join two strings due to Segmentation Fault

This is a discussion on Unable to join two strings due to Segmentation Fault within the C++ Programming forums, part of the General Programming Boards category; Hi, I have two .txt files. I am trying to combine each line from the first file with each line ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Unable to join two strings due to Segmentation Fault

    Hi,

    I have two .txt files.

    I am trying to combine each line from the first file with each line from the second file, using a nested loop:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main ()
    {
        string keyword = "";
        vector<string>vectorOfStrings;
        ifstream ifs( "anchortxt.txt" );
        string temp;
    
        while( getline( ifs, temp ) )
    
        {
            vectorOfStrings.push_back( temp );
        }
    
        vector<string>vectorOfStringsTwo;
        ifstream ifss( "rhwdtieronearticles.txt" );
        string temps;
    
        while( getline( ifss, temps ) )
        {
            vectorOfStringsTwo.push_back( temps );
        }
    
        for (unsigned int i=0; i<=vectorOfStrings.size(); i++)
        {
    
    
            for (unsigned int j=1; j<=i; j++)
            {
                string aa = vectorOfStrings[i] + vectorOfStringsTwo[j];
                cout << aa << endl;
                aa.clear();
            }
        }
    
        return 0;
    }
    I am using Code::Blocks on Ubuntu 64bit.

    When I try to build and run the program I get the following:

    Segmentation fault
    The two text files contain strings/lines which are around 500 characters in length and contain many different characters such as % | ' , .

    When I run the program on txt files that contain just around 30 chars per line, with just a-z characters in, it runs fine.

    Can anyone suggest why this is occurring?

    Many thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    There is a big difference between < and <=.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    To elaborate, the error is here:
    for (unsigned int i=0; i<=vectorOfStrings.size(); i++)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Now, being C++ there are safer and more effective ways of doing this using iterators. Additionally, I am assuming since you are using std::vector here this isn't a backwards school project so let's take a look at another possible implementation to ease your efforts.
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    /*used for stream iterators*/
    #include <iterator>
    #include <algorithm>
    
    /*define our datatype*/
    struct fileLine{
    	std::string input;
    };
    /*override iterator for our datatype*/
    std::istream& operator>>(std::istream& stream, fileLine& line){
    	//return getline method
    	return (std::getline(stream,line.input));
    }
    int main(void){
    
    	//our two vectors of type fileLine
    	std::vector<fileLine>stringvect1, stringvect2;
    	std::ifstream file1("example1.txt");
    	std::vector<fileLine>::iterator stringvect_it;
    
    	/*Read files into our vectors, line by line using
    	stream iterators*/
    	std::copy(std::istream_iterator<fileLine>(file1),
    		std::istream_iterator<fileLine>(),
    		std::back_inserter(stringvect1));
    	/*you could do the same thing with your other file*/
    
    	/*now copy the contents of our vector out. It is possible to 
    	use std::copy here as well, however we will use an iterator
    	for demonstration purposes*/
    	stringvect_it = stringvect1.begin();
    	while(stringvect_it != stringvect1.end()){
    		std::cout << stringvect_it->input <<std::endl;
    		++stringvect_it;
    	}
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when appending to strings (char *)
    By LanguidLegend in forum C Programming
    Replies: 15
    Last Post: 02-24-2011, 06:35 PM
  2. String problems - strings seem to join?
    By Metalixia in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2007, 10:00 AM
  3. Join 2 strings inside 'fopen'?
    By tomas.ra in forum C Programming
    Replies: 5
    Last Post: 09-24-2005, 04:08 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  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