aborted core dumped

What does this error mean? I singled it down the me using string.substr(), but i dont see the error. I put a small snippet up testing that for an error, but i get the expected results with:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <vector>
#include <string>
int main(){
    std::string s = "t t print('tester')";
    int ind = s.find("print");
    std::cout << ind << std::endl;
    std::cout << s.substr(ind) << std::endl;
}

which prints out:
1
2
4
print('tester')

and te last line being what i expected from the other code


I am not sure whats different from this code snippet and this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <vector>


namespace util{
    std::vector<std::string> vars;
    
    //write vector to file by element to line
    void write(std::string filename, std::vector<std::string> v, std::string sep="\n"){
        std::ofstream f;
        f.open(filename);
        for (std::vector<int>::size_type i = 0; i != v.size(); i++) {
            f << v[i] << sep;
        }
        f.close();
    }
    
    //pull file to vector by line to element
    std::vector<std::string> open(std::string filename){
        std::string s = "";
        std::vector<std::string> v;
        std::ifstream f;
        f.open(filename);
        while(getline(f, s)){
            v.push_back(s);
        }
        f.close();
        return v;
    }
    
    //check if filename exist
    bool file_exists(std::string filename){
        std::ifstream f(filename);
        if (f)
            return true;
        else
            return false;
    }
    
    //check if argv[1] exists
    bool arg_is_file(int argc, char **argv){
        if (argc >= 2){
            if (file_exists(argv[1])){
                return true;
            }
            else{
                std::cout << "Filename: \"" << argv[1] << "\" does not exist" << std::endl;
                return false;
            }
        }
        else{
            std::cout << "Program requires a filename for argument 1\n";
            return false;
        }
    }
    
    void print(std::string arg){
        std::cout << arg;
    }
}


int main(int argc, char **argv){
    int ind;
    std::string line;
    
    if (util::arg_is_file(argc, argv)){
        std::vector<std::string> filer = util::open(argv[1]);
        for (auto line: filer){
            if (line != ""){ //ignore empty lines
                if (line.find("print")){
                    ind = line.find("print");
                    std::cout << line.substr(ind);
                }
                std::cout << line << "|";
            }
        }
        
        
            
        std::cout << std::endl;
    }
}


and the input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
metulburr@ubuntu:~/programs/cplusplus$ g++ -std=c++11 test.cpp -o test
metulburr@ubuntu:~/programs/cplusplus$ ./test test.simp
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
print('hello world')|Aborted (core dumped)
metulburr@ubuntu:~/programs/cplusplus$ cat test.simp
print('hello world')
a = 1
def func():
    localvar = 0


test_whitespace = "test"
metulburr@ubuntu:~/programs/cplusplus$ 
Last edited on
This code

1
2
3
4
                if (line.find("print")){
                    ind = line.find("print");
                    std::cout << line.substr(ind);
                }


is invalid. If find does not find a string it retursn non-sero value. So this condition if (line.find("print")) will be equal to true. The correct if statement will look as

1
2
3
4
                if (line.find("print") != std::string::npos ){
                    ind = line.find("print");
                    std::cout << line.substr(ind);
                }

ah ok thanks vlad
Topic archived. No new replies allowed.