Hello all,

I have a text file called: concentrations.dat

The file contains a lot of different data. Here's the data that I'm interested in:

# ET: list of elements
'Cu' 'C' 'Sr' 'He' 'Mg' 'O' 'Cr'

What I want to do is read the elements into a vector called elementVector. So I would end up with Cu, C, Sr, He, Mg, O, and Cr as elements of the vector without the ' symbols.

Can anyone tell me how to do this? An example would be much appreciated. I have had no success with using ifstream. I can read in an entire file, but I can get parts of it out!

I could either read in the entire file or only read in the part that I wrote above. Then I would need to get the elements, while discarding the ' symbols.

I need to do this because I have written a program for doing calculations that needs to know which chemical elements are involved.

Thanks for any help.

7 Years
Discussion Span
Last Post by RobBobSmith

Probably something like this (untested) code

#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main()
    std::vector<std::string>  elementVector;
    std::ifstream in("filename.dat");
    std::string line;
    while( std::getline(in, line) )
        if( line == "<et>" )
            stringstream st;
            st << line;
            std::string word;
            while( std::getline(st,word,'\'' ))
                size_t pos = word.find('\'');
                if( pos != string::npos )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.