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.

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 )


Thanks a lot Ancient Dragon! This should do the job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.