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

  • SEARCH
  • Home
  • 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 6064943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:20:07+00:00 2026-05-23T09:20:07+00:00

I am running C++ code where I need to import data from txt file.

  • 0

I am running C++ code where I need to import data from txt file.
The text file contains 10,000 lines. Each line contains n columns of binary data.

The code has to loop 100,000 times, each time it has to randomly select a line out of the txt file and assign the binary values in the columns to some variables.

What is the most efficient way to write this code? should I load the file first into the memory or should I randomly open a random line number?

How can I implement this in C++?

  • 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-23T09:20:07+00:00Added an answer on May 23, 2026 at 9:20 am

    To randomly access a line in a text file, all lines need to have the same byte-length. If you don’t have that, you need to loop until you get at the correct line. Since this will be pretty slow for so much access, better just load it into a std::vector of std::strings, each entry being one line (this is easily done with std::getline). Or since you want to assign values from the different columns, you can use a std::vector with your own struct like

    struct MyValues{
      double d;
      int i;
      // whatever you have / need
    };
    
    std::vector<MyValues> vec;
    

    Which might be better instead of parsing the line all the time.

    With the std::vector, you get your random access and only have to loop once through the whole file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been running this code (from: http://blog.somethingaboutcode.com/?p=155 ): from twisted.internet import reactor from
each unit test I'm running is writing python code out to a file, then
I need to know how to turn on Code Coverage when running TFS builds
Hello friends i am running code given below which contains the setLogTimeEntery function and
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
The code #!/usr/bin/env python import MySQLdb print Content-Type: text/html print print <html><head><title>Books</title></head> print <body>
I am trying to capture from a Sony Handicam DCR-HC38, running the code below,
I need to import a very large dictionary into python and I'm running into
I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
We get an abort when a C++ exception is thrown when running code compiled

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.