C ++ rookie (streams) ... How to read lines from a file where the line has a space (e.g. Tom Smith)? Problem using getline instead of >>

I wrote a template function to read in string or numeric data from files and save data in row vectors or int / double. Then I use the data to perform calculations with another code that I wrote.

Advance apologies because I think this is a simple question ... I cannot read in string data where there is a space ... For example, first and last name. When I want Tom Smith, I only get Tom.) From googling, it seems the problem is → and that I should use getline instead. I tried replacing -> getline (test, 100), but I get an error like "no match for calling in std :: basic_istringstream ..." (error: there is no corresponding function for calling "std :: basic_ifstream> :: getline (double & )))

I would be very grateful if someone could fix me! I just can't seem to have my head around the streams!

This is sample data and my code. I configured it for the lines here.

labelInFile // Identifier of the subset of data for one vector

'Tom Smith' 'Jackie Brown' 'John Doe' // These names must be in the form of elements in the 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; } 
+4
source share
2 answers

I wrote code to solve part of your problem: name analysis. You can adapt it to your needs. Note. This is not the fastest way to solve this problem, but it is a simple way that we hope is 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' 'VS 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 VS Naipaul Vladimir Nabokov William Shakespeare William Langland Robert Greene 
+1
source

The following is a snippet of fileRead . I changed only line (1) . With this change, as far as I can see, your code works the way you expect.

 do{ .... std::istringstream myStream( line ); while ( myStream >> test ) // (1) results.push_back( test ); } while ( file.get() != '\n' ) 

Hope this helps

0
source

Source: https://habr.com/ru/post/1344363/


All Articles