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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:27:57+00:00 2026-05-20T02:27:57+00:00

I need to read in numbers from an external file and store them in

  • 0

I need to read in numbers from an external file and store them in a vector of ints. I can do this now thanks to Howard Hinnant and wilhelmtell, who patiently helped figure out why my coding was not working yesterday.

I have been trying to figure out how to incorporate an additional feature into the code, but I have exhausted my knowledge of streams and would appreciate some advice.

I want to be able to deal with files containing many sets of data. Is it possible to extract only certain data from the file into a vector? I want to end up with several vectors that contain data from different parts of the file. I searched online, but have not seen any mention of this.

Here’s the code plus an example of a file (let’s call it “test”) that I want to get data from.


Edit: I edited the code based on CashCow’s advice. I can now get a block out of the data file. But I don’t know how to get the block I want. If I run the code as it is, I get a vector that contains the elements 2,5,8 (this is not what I want). To get vectorTwo (4,5,6 in the example I made), I tried adding this around the while statement:

if( line == "vectorTwo" )
{
      // The while statement and its contents
}

It did not work. I did not get any results from running the code (it compiled though). Can anyone see what the problem is? I figured I could use this statement to search for the header for the block of data I need.


//Here are the contents of the example file

vectorOne // Identifier for subset of data for one vector

‘1’ ‘2’ ‘3’

vectorTwo // How would I get this one vector? Or any other one vector?

‘4’ ‘5’ ‘6’

vectorThree // Identifier for subset of data for another vector

‘7’ ‘8’ ‘9’

// Code:
The ‘\” character is the line delimiter. Everything is ignored up to the first ‘ and then everything until the next ‘ is part of a number. This continue until the logic fails (end of file). How can I get it to stop at the end of a data block instead?

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

int main()
{
    std::string line;
std::string block;         // Edited, from CashCow
    const std::string fileName = "test.dat";  

    std::ifstream theStream( fileName.c_str() ); 

    if( ! theStream )
          std::cerr << "Error opening file test.dat\n";

    std::vector<int> numbers;  // This code is written for one vector only. There would be three vectors for the example file I provided above; one for the vectorOne data in the file, and so on.

    while (true)
    {
        // get first '
        std::getline(theStream, line, '\'');
        // read until second '
        std::getline(theStream, line, '\'');
        std::istringstream myStream( line );

        std::getline( theStream, block, '\n' );  // Edited, from CashCow
        std::istringstream blockstream( block ); // Edited, from CashCow
        std::getline(blockstream, line, '\'');   // Edited, from CashCow


        int i;
        myStream >> i;
        if (myStream.fail())
            break;
        numbers.push_back(i);
    }
    std::copy(numbers.begin(), numbers.end(),
              std::ostream_iterator<int>(std::cout, "\n"));
}
  • 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-20T02:27:58+00:00Added an answer on May 20, 2026 at 2:27 am

    When you reach the start of a block just read to the end of the line into a string, then parse the string.

    std::getline( theStream, block, '\n' );
    std::istringstream blockStream( block );
    std::getline( blockStream, line, '\'' ); // etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read some numbers (double) from a file and store them in
I need to read the data from a file that can be either comma
I am very new to Python, I need to read numbers from a file
I need to read a number of arrays from a file and print them.
I need to read small sequences of data from a 3.7 GB file. The
I need to get very basic input from an external file in C++. I
i need to read organization name from phone contacts in 2.1 i can read
I want to read in a grid of numbers (n*n) from a file and
I need to build a content gathering program that will simply read numbers on
I need to read and serialize objects from and to XML, Apple's .plist format

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.