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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:25:15+00:00 2026-05-20T10:25:15+00:00

To use code I have written for performing calculations, I need to read in

  • 0

To use code I have written for performing calculations, I need to read in data (numbers and strings) from an external text file and store them in vectors of either strings or ints/doubles. I have written a template function for doing this. CashCow, Howard Hinnant, and wilhelmtell kindly helped with a previous problem.

The function seems to work fine for ints/doubles, but I have a problem with string data.

I need data from ONE line of my external file to go into a vector, but the function reads in multiple lines. Here’s what I mean. Let’s say this is what is in the external text file (below):


vectorOne // Identifier for subset of data for one vector

‘1’ ‘2’ ‘3’ // These values should go into one vector, (vectorOne)

vectorTwo // Identifier for subset of data for another vector (vectorTwo)

‘4’ ‘5’ ‘6’ // These values should go into a different vector

vectorThree // Identifier for subset of data for another vector (vectorThree)

‘7’ ‘8’ ‘9’ // These values should go into a different vector


If I look for a data subset identifier/label (like vectorOne), I want only the data on the next line to go into my result vector. The problem is that ALL data below the identifier/label are ending up in the result vector. So if vectorTwo is what I want, I expect my result vector to contain the elements, “4, 5, 6.” But intead, it contains 4 to 9. In my code (below), I thought that the line:

while ( file.get() != '\n' );

ensured that the read would stop at a newline (i.e., after each line of data).

I would be very grateful for any suggestions as to what is going wrong.

Here’s the code (for clarity, I configured it for strings):

#include <algorithm>  
#include <cctype>     
#include <istream>
#include <fstream>
#include <iostream>    
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

using namespace std; 

template<typename T>  
void fileRead( std::vector<T>& results, const std::string& theFile, const std::string& findMe, T& temp )  
{   
    std::ifstream file( theFile.c_str() ); 
    std::string   line;

    while( std::getline( file, line ) )
    {
        if( line == findMe )
        {
            do{
                std::getline( file, line, '\'' );  
                std::getline( file, line, '\'');

                std::istringstream myStream( line );

                myStream >> temp;
                results.push_back( temp );
            } 
            while ( file.get() != '\n' );
        }
    }
}


int main () 
{
    const std::string theFile               = "test.txt";  // Path to file
    const std::string findMe                = "labelInFile"; 
    std::string temp;

    std::vector<string> results;

    fileRead<std::string>( results, theFile, findMe, temp );

    cout << "Result: \n";
    std::copy(results.begin(), results.end(), std::ostream_iterator<string>(std::cout, "\n")); 

    return 0;
}

Thanks

  • 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-20T10:25:15+00:00Added an answer on May 20, 2026 at 10:25 am

    Looks to me like you may have a problem mixing getline and get.

    When you’ve read the name of the vector you want, you start reading the parts between single quotes. Once you’ve read whatever is between single quotes, you check to see if the next character is an end of line. If there’s anything else on the line before the newline, the test fails and it reads whatever is between the next pair of single quotes. If there’s a comment at the end of the line, or a space, or anything at all past the last single quote, what you’ve got will fail.

    Try reading the whole line into a string, and then reading it as a stringstream. That way, you can’t go past the end of line.

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

Sidebar

Related Questions

I have a console application that require to use some code that need administrator
Normally I create web application projects and use code-behind, but I have a requirement
What should i use to code Classic ASP under Linux. I have really tried
Does anyone have sample code to copy open (in-use and locked by another program)
I am trying to use the following code, which I have not been able
According to what I have found so far, I can use the following code:
I'm trying to use opengl in C#. I have following code which fails with
I have to use a third-party component without source code. I have the release
I have some Python code that works correctly when I use python.exe to run
Let's say I have the following ruby code : def use_object(object) puts object.some_method end

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.