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 636173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:29:10+00:00 2026-05-13T20:29:10+00:00

I have a c++ program which performs one function. It loads a large data-file

  • 0

I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so:

$ ./myprogram 1 2 3 4 5 6 7

I also have a ruby script, and I would like this script to utilize the c++ program.
Currently, I am doing this like so.

Ruby Code:

arguments = "1 2 3 4 5 6 7"
an_integer = %x{ ./myprogram #{arguemnts} }
puts "The program returned #{an_integer}" #=> The program returned 2283

This is all working properly, but my problem is that each time ruby makes this call, the c++ program has to reload the data-file (which is over 100mb) – very slow, and very inefficient.

How can I rewrite my c++ program load the file only once, allowing me to make many lookups via a ruby script without reloading the file each time. Would using sockets be a sensible approach? Writing the c++ program as a ruby extension?

Obviously I am not an experienced c++ programmer, so thanks for your help.

  • 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-13T20:29:11+00:00Added an answer on May 13, 2026 at 8:29 pm

    A possible approach is to modify your C++ program so that it takes its input from the standard input stream (std::cin) instead of from the command line parameters, and returns its result through the standard ouput (std::cout) instead of as main’s return value. Your Ruby script would then use popen to launch the C++ program.

    Assuming the C++ program currently looks like:

    // *pseudo* code
    int main(int argc, char* argv[])
    {
        large_data_file = expensive_operation();
    
        std::vector<int> input = as_ints(argc, argv);
        int result = make_the_computation(large_data_file, input);
    
        return result;
    }
    

    It would be transformed into something like:

    // *pseudo* code
    int main(int argc, char* argv[])
    {
        large_data_file = expensive_operation();
    
        std::string input_line;
        // Read a line from standard input
        while(std:::getline(std::cin, input_line)){
            std::vector<int> input = tokenize_as_ints(input_line);
            int result = make_the_computation(large_data_file, input);
    
            //Write result on standard output
            std::cout << result << std::endl;
        }
    
        return 0;
    }
    

    And the Ruby script would look like

    io = IO.popen("./myprogram", "rw")
    while i_have_stuff_to_compute
        arguments = get_arguments()
        # Write arguments on the program's input stream
        IO.puts(arguments)
        # Read reply from the program's output stream
        result = IO.readline().to_i();
    end
    
    io.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code on the original question relies on several assumptions… May 14, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer Use an instance variable: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { private String songtoplay;… May 14, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer You have these two variables labelled incorrectly: var mathfloor: <span… May 14, 2026 at 4:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.