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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:14:37+00:00 2026-05-20T18:14:37+00:00

I wrote a template function for reading in string or numerical data from files

  • 0

I wrote a template function for reading in string or numerical data from files and saving the data in vectors of either strings or ints/doubles. I then use the data to perform calculations with another code I wrote.

Advance apologies, because I think this is a simple question… I can’t read in string data where there is a whitespace… For example, a first and last name. When I want “Tom Smith,” I only get “Tom”). From googling, it seems that the problem is >> and that I should use getline instead. I have tried replacing >> with getline(test,100), but I’m getting a “no matching function for call to std::basic_istringstream…” type error ( error: no matching function for call to ‘std::basic_ifstream >::getline(double&)’)

I would be very grateful if someone could put me right! I just can’t seem to get my head around streams!

This is some example data and my code. I configured it for strings here.

labelInFile // Identifier for subset of data for one vector

‘Tom Smith’ ‘Jackie Brown’ ‘John Doe’ // These names should end up as elements in a vector

#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& test )  
{   
    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 >> test;
                results.push_back( test );
            } 
            while ( file.get() != '\n' );
        }
    }
}


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

    std::vector<string> results;

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

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

    return 0;
}
  • 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-20T18:14:38+00:00Added an answer on May 20, 2026 at 6:14 pm

    I’ve written some code to solve part of your problem: parsing the names. You can adapt it to your needs. Note: this is not the fastest way to solve this, but it is a simple way that is hopefully easy to understand.

    Test.cpp

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <vector>
    
    int main()
    {
        const char szFname[] = "Test.dat";
        std::vector<std::string> vData;
    
        std::ifstream ifstr(szFname);
    
        while (ifstr.good())
        {
            // find first quote
            ifstr.ignore(0xffff, '\'');
            std::string sData;
            char ch;
            while (ifstr.good() && ('\'' != (ch=ifstr.get())))
                sData += ch;
            if (!sData.empty())
                vData.push_back(sData);
        }
        for (size_t i=0; i<vData.size(); ++i)
            std::cout << vData[i] << std::endl;
    
        return 0;
    }
    

    Test.dat

    'Tom Smith' 'Jackie Brown' 'John Doe' 
    'Robert Burns'   'James Joyce''Joseph Conrad' 'Dylan Thomas'
    'Edgar Allan Poe' 'V.S. Naipaul' 'Vladimir Nabokov'
    'William Shakespeare'  'William Langland' 'Robert Greene'
    

    Results

    Tom Smith
    Jackie Brown
    John Doe
    Robert Burns
    James Joyce
    Joseph Conrad
    Dylan Thomas
    Edgar Allan Poe
    V.S. Naipaul
    Vladimir Nabokov
    William Shakespeare
    William Langland
    Robert Greene
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
I wrote a windows service using VB that read some legacy data from Visual
I wrote a template function to compare two variables: template <class t> int compare(const
I recently wrote a function template which takes a reference to a C-array: template
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
In C++0x I would like to write a function like this: template <typename... Types>
I come from a C++ background where I can use template mixins to write
I have been reading templates,functors,callback function for the past week and have referred some
I'm having a annoying problem with a C++ function that I wrote and whose
I wrote a template class which is giving compilation error template<class T> class Entity

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.