Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4105604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:16:09+00:00 2026-05-20T21:16:09+00:00

Hi I have the following c++ program: #include <iostream> #include <fstream> #include <vector> #include

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:16:10+00:00Added an answer on May 20, 2026 at 9:16 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>
i have following program to calculate size of file #include <iostream> #include <fstream> #include
I have the following program test.cc: #include <iostream> unsigned char bogus1[] = { //
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
I have the following piece of code in C++: #include <iostream> #include <fstream> #include
I have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
I have the following code taken from cplusplus.com : // set_terminate example #include <iostream>
I have a problem with the following code: #include <stdio.h> #include <iostream> int main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.