Hello all:

I have written a function that gets data (numbers) from a text file.

My function works by taking a line from the text file, discarding unnecessary symbols, and putting the numbers into a vector. Here's a snippet:

// Get data from text file. Values then put into one vector.

if( line == theData ){
std::getline( file, line );
line.erase(remove( line.begin(), line.end(), '\'' ), line.end() );
std::istringstream streamLine( line );
}

The problem is, the text file looks ugly. I have to put the data on a single line. There are labels on the line above. When there are many numbers, the labels and the data are wrapped around to the next lines. So it is very hard for a person using the file to tell which label goes with which number on the line below.

What I want to do is have a list (vertical) of labels with the correct numbers next to them. But I'm not sure if this would be possible. From what I have seen, it seems like data always have to be on the same line. I need a result like:

Apple: 1
Orange: 2
Banana: 3

Result vector has elements 1,2,3

Thanks for any help.

Dani AI

Generated

A more robust way to handle wrapped labels/data is to stop treating the file as “one label line, one data line” and instead treat the file as a token stream. Scan tokens left-to-right, classify each token as a label (text) or a numeric value, collect all label tokens that come before the first numeric token, then collect numeric tokens until the expected number is reached. That approach tolerates arbitrary line breaks (wrapping) because operator>> ignores whitespace and line breaks — the physical newlines no longer matter.

The example below shows that idea: classify tokens with a small numeric-check helper, accumulate labels until the first number appears, then accumulate numeric values and finally pair them.

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

bool isNumber(const std::string &s, double &out) {
    std::istringstream iss(s);
    if (!(iss >> out)) return false;
    char c;
    return !(iss >> c); // ensure entire token was numeric
}

int main() {
    std::ifstream in("data.txt");
    std::vector<std::string> labels;
    std::vector<double> numbers;
    std::string tok;
    bool numbers_started = false;

    while (in >> tok) {
        double v;
        if (isNumber(tok, v)) { numbers_started = true; numbers.push_back(v); }
        else if (!numbers_started) labels.push_back(tok);
        else { /* unexpected text after numbers: ignore or handle */ }
    }

    for (size_t i = 0; i < labels.size(); ++i)
        std::cout << labels[i] << ": " << (i < numbers.size() ? std::to_string(numbers[i]) : "MISSING") << '\n';
}

Notes and troubleshooting: this works when labels are single tokens (no internal spaces). If labels may contain spaces, require a delimiter (comma, tab) or quote labels so they remain one token. Strip trailing punctuation (commas, colons) before numeric checks. Do not use loops of the form while(!file.eof()) (see ) — read with >> and test the extracted value. As pointed out, exact behavior depends on the real input; adding a consistent delimiter or a small header that states the number of fields makes parsing unambiguous and much simpler.

Recommended Answers

All 4 Replies

Seems awfully similar to this thread.
Anyway, post all the code that you have written so far. Not only snippets. And a sample of the input text file. Not just the required output.

That would be my brother. We're both stuck on streams. Any resources/texts would be good to know about.

how to access documents from "D" and then i want to store them in queu and then print...plz answer me

Try this one....
Maybe its help....

 ifstream Read;
 Read.open("text.txt");
 char output[100];
 if (Read.is_open()) {
 while (!Read.eof()) {


    Read >> output;
    cout<<output;


 }
}
Read.close();
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.