Hello everyone:

I'm a C++ newbie.

I have written code for performing calculations that needs data from another code. These data are in text files. I know how to read in an ENTIRE text file, but I can't figure out how to extract certain pieces of it. I would very much appreciate any help and an example would be very welcome. I thought of using ignore and getline from istream, but I can't figure out how to extract only the information I need.

Below is an example of what is in each text file. I need to extract the names of the elements only into a vector/container. So I need O, W, Re, H, He, K, Na, Y, and O--not the '' symbols. The position of each element in the vector will then be used to perform further calculations.

File is called output. Here's what is in it:

## (5) Elements section
# OVMZ: Name list of elements
<OVMZ>
'O' 'W' 'Re' 'H' 'He' 'K' 'Na' 'Y' 'O'

Dani AI

Generated

Good call, — the element names are quoted, so detecting quote-delimited tokens is the right approach. was on the right track. Below is a compact, robust C++11 solution that reads the file line-by-line, extracts every substring between single quotes, and stores them in a vector in the same order they appear (duplicates preserved, which keeps positions stable for later calculations).

#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>

int main() {
    std::ifstream fin("output");
    if (!fin) return 1;

    std::vector<std::string> elements;
    std::string line;
    std::regex re("'([^']+)'"); // capture text between single quotes

    while (std::getline(fin, line)) {
        std::smatch m;
        std::string s = line;
        while (std::regex_search(s, m, re)) {
            elements.push_back(m[1].str());
            s = m.suffix().str();
        }
    }

    for (const auto &e : elements) std::cout << e << '\n';
}

Notes and troubleshooting: the pattern "'([^']+)'" captures any non-empty run inside single quotes. If your file has header lines (tags), skip lines until you reach the one with quotes (e.g., check line.find('\'') != std::string::npos or look for the tag). If you need unique element names, use std::sort + std::unique or an std::unordered_set — but that will change indices, so only do that if you no longer need original positions.

If your compiler/library lacks reliable <regex> support (older toolchains), fall back to a simple character scan: find the next ', accumulate chars until the next ', push the token, and repeat — but make sure to guard against unmatched quotes and end-of-file. Also watch for CRLF or encoding issues if matches fail unexpectedly.

Hello everyone:

I'm a C++ newbie.

I have written code for performing calculations that needs data from another code. These data are in text files. I know how to read in an ENTIRE text file, but I can't figure out how to extract certain pieces of it. I would very much appreciate any help and an example would be very welcome. I thought of using ignore and getline from istream, but I can't figure out how to extract only the information I need.

Below is an example of what is in each text file. I need to extract the names of the elements only into a vector/container. So I need O, W, Re, H, He, K, Na, Y, and O--not the '' symbols. The position of each element in the vector will then be used to perform further calculations.

File is called output. Here's what is in it:

## (5) Elements section
# OVMZ: Name list of elements
<OVMZ>
'O' 'W' 'Re' 'H' 'He' 'K' 'Na' 'Y' 'O'

1) Look for first instance of a quotation mark.
2) This signifies the start of the element name.
3) Record everything until the next quotation mark, which will be the end.
4) Repeat this process until end of file.

Will your container be holding the names of the elements as strings? If so, try something like this (untested):

.
.
.
vector<string> Elements;
string element = "";
while (fin.get() != EOF){

  while (fin.get != '\'') //open quote. the escape sequence \' allows you to look for '
    {fin.ignore(1);}  //ok found the open quote, ignore that character

  while (fin.get() != '\'') //so now we're in.  record chars while no close quote
    {element += fin.get();} //push characters 1 at a time onto a string until close quote

  Elements.push_back(element);
  element.clear(); //reset the string for next time until EOF.  clear is a member function of string right?  can't remember.  Else set it to "".
}

-Greywolf

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.