Problem reading delimited int data from text file using stream

I have text files that contain names and numerical data. I want to read both kinds of data into vectors. The code works for strings (produces a vector of string data from the text file), but it does not work for the numerical data (does not produce a vector of ints). When I run it, there is nothing. I can't figure out the problem.

Example of the kind of numerical data I want to extract from the text file test.txt:

identifier
'2' '3' '4' '5'

I want a vector of ints called "results" that has the elements, 2, 3, 4, 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept> 
#include <iterator>
#include <algorithm>  
#include <sstream>

template<typename T>
std::vector<T> readData( const std::string& fileName )
{
	std::ifstream file( fileName.c_str() );
	std::string   line;
	
	while( std::getline( file, line ) )
	{
		if( line == "identifier" ) 
		{
			std::getline( file, line );
			line.erase(remove( line.begin(), line.end(), '\'' ), line.end() );
			std::istringstream streamLine( line );
			
			return std::vector<T>( std::istream_iterator<T>(streamLine),
								  std::istream_iterator<T>() );
			
		}
	}
	return std::vector<T>();
}

void stringsMethod(std::vector<std::string> &results, const std::string& fileName )
{
	results = readData<std::string>( fileName );
}

void intsMethod(std::vector<int> &results, const std::string& fileName )
{
	results = readData<int>( fileName );
}




int main () 
{	
	const std::string fileName               = "test.txt"; 
	
	// std::vector<string> results;  
	std::vector<int> results;
	
	//stringsMethod( results, fileName );  // Works
	intsMethod( results, fileName );
	
    return 0;
}



I would be grateful for any help!
Last edited on
Strictly speaking I didn't find any problem except the syntactical one - intsMethod has one parameter less, namely it has no LookFor. I don't know what is the purpose of LookFor. Either remove it form stringsMethod and skip it from the function calls, or add it to intsMethod and provide appropriate value. May be it was intended to provide another tag instead of "identifier"?

Anyways, those syntax stuff aside, the program works on my pc with the test you provide. (mingw gcc compiler)

Regards
Thanks simeonz. That was a typo. I edited the question.

You actually get a vector of ints that has the numbers as elements?

Like I said, I can compile and run the program, but I get no results for numerical values. I can read in strings and get a vector of those, but not numbers.

I have gcc 4.2. I wonder if it is a compiler thing?
To be absolutely sure that it does not have to do something with the compiler switches that my ide feeds in, I did the following. Took your code, pasted it in main.cpp and inserted the following at the end of main:
1
2
3
4
for (int i = 0; i < results.size(); ++i)
{
std::cout << results[i];
}

Then copied your example in test.txt. (No empty line at the end of the file.) Then compiled with
g++ main.cpp -o main.exe

and after launching the output was
2345

Another info: mingw g++ 4.5.0

Regards
Thanks for taking the trouble to do that, simeonz. I guess it's time for me to learn about compilers!
P.S.... If you or anyone else could suggest anything in my code that might be problematic for certain compilers, that I could replace with different code, I would be very grateful. I'm not experienced with streams!
Topic archived. No new replies allowed.