0

Hi I have the following c++ program:

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <boost/foreach.hpp>
#include <stdexcept>
#include <boost/flyweight.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

struct entry
{
int file;
std::vector<double> a;
};


void my_file(const std::string&file, std::vector<entry> &data, int i){
try{
    std::ifstream in(file.c_str());
    entry e;
    std::string line;
    e.file = i;
    while(getline(in,line)){
        try{
            data[i].a.push_back( boost::lexical_cast<double> (line));
        }catch(boost::bad_lexical_cast bad){
            //std::cerr << bad.what() << std::endl;
        }
    }
}catch(std::runtime_error err){
    std::cerr << err.what() << std::endl;
}

}

void write_file(const std::string &file,std::vector<entry> data,const char* t_path){
try{
    std::string new_file = t_path ;
    new_file = new_file + "/" + file;
    std::ofstream f(new_file.c_str());

    for(size_t i = 0 ;i < data[1].a.size();i++){
        std::cout << "i: " << i;
        for(size_t j = 1; j < data.size();j++){
            std::cout << "j: " << j << std::endl;
            f << data[j].a[i]<< "\t";
        }
        f << "\n";
    }

}catch(std::runtime_error err){
    std::cerr << err.what()<< std::endl;
}
}


int collect_peak(const char*argv,const char*out){
std::cout << "collecting peaks\n";
std::stringstream sstr(argv);
std::string _line;
int c = 0;
std::vector<std::string> files;

while (getline(sstr,_line)){
    std::cout << _line << std::endl;
    fs::path p(_line);
    std::string tmp = p.parent_path().string() +"/_peak_" +      p.filename().string();
    files.push_back(tmp);
    c++;
}

std::cout << "c: " << c << std::endl;
std::vector<entry> data;
for (int i=0 ; i < files.size() ;++i){
    std::cout << files[i] <<std::endl;
    my_file(files[i],data,i);
}
write_file("__peak.txt",data,out);
return 0;

}

Somehow it always gives me a bad access in the my_file method. The program should actually to the following:

  1. read multiple files that contain a caption and ten doubles seperated by a newline
  2. output everything into one file so it looks like this:

    1. file \t 2. file \t 3. file \t ...

    2. file \t 2. file \t 3. file \t ...

    3. file \t 2. file \t 3. file \t ...

    4. file \t 2. file \t 3. file \t ...

    5. file \t 2. file \t 3. file \t ...

This actually worked already, but i reused it now in a different program. Any ideas?

Thank you

2
  • Did you double check your input files? your cast to double might be failing and causing the issue. Commented Mar 17, 2011 at 17:13
  • I did;) i often had that problem but thanks Commented Mar 17, 2011 at 17:23

1 Answer 1

5

This line:

std::vector<entry> data;

creates an empty vector, which you are passing to my_file and accessing data[i] within. You need to reserve space for the elements before you can access arbitrary indices in the vector. For example:

std::vector<entry> data(maxItems);
Sign up to request clarification or add additional context in comments.

3 Comments

thats what i was using the "c" variable for like this std::vector<entry> data(c), but then I'm getting an error in the write_file method,
I tried this: std::vector<entry> data(c); for (int i=0 ; i < c ;++i){ but now it tells me this: pointer being freed was not allocated
finally figured it out: it was one missing & for the data vector in the arguments of write file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.