Hi there,

I need to read a file with unknown number of columns and lines. Basically, I have an ID per line (ID00005) and many columns containing 2 letters. The format is:

ID00005 RR WW SS QQ TT RR UU II
ID00008 WW SS QQ YY HH JJ KK LL
ID00011 WW SS QQ YY HH JJ KK LL
...

I don't known how to read line by line, getting column by column after ID (RR WW SS QQ TT ...) and do some calculations using the column information (RR WW). The calculation is count the number of occurrence of each two letters Example line 1 (RR = 2 times)

I only know how to get ID and number of columns that I can count

The idea is read the first line, do the calculation based on the elements of columns and after that, go to the next line

Can anyone help me?

Thanks a lot!

string ID = "";

if ( myfile.is_open() )
	{
		while(myfile >> lineString)
		{
			ID = lineString;
			myfile >> lineString;
                        // ??????????????????????????
			
		}
	}
	else
		cout << "Unable to open file." << endl;

Dani AI

Generated

Good quick solution from and well done for spotting the fix. The next useful step is a small, robust processing pattern that (a) isolates the ID, (b) validates / normalizes each two-letter column, (c) produces per-line counts and a running total across the file, and (d) handles stray whitespace or punctuation.

A reliable algorithm:

  • Read the file line by line with getline.
  • Extract the first white-space token as the ID.
  • For each following token: strip non-alpha, convert to uppercase, skip if length != 2.
  • Count occurrences in a per-line map and also increment a global map.
  • Emit per-line counts in a deterministic order (sort keys) so results are reproducible.

Example implementation (shows counting, normalization and totals):

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cctype>

static std::string normalize(const std::string &s){
    std::string out;
    for (unsigned char ch: s)
        if (std::isalpha(ch)) out.push_back(std::toupper(ch));
    return out;
}

int main(){
    std::ifstream file("input.txt");
    std::string line;
    std::unordered_map<std::string,int> totals;

    while (std::getline(file, line)) {
        if (line.empty()) continue;
        std::istringstream iss(line);
        std::string id;
        if (!(iss >> id)) continue;

        std::unordered_map<std::string,int> perline;
        std::string tok;
        while (iss >> tok) {
            std::string code = normalize(tok);
            if (code.size() != 2) continue;
            ++perline[code];
            ++totals[code];
        }

        std::vector<std::pair<std::string,int>> items(perline.begin(), perline.end());
        std::sort(items.begin(), items.end());
        std::cout << id;
        for (auto &p : items) std::cout << ' ' << p.first << ':' << p.second;
        std::cout << '\n';
    }

    std::vector<std::pair<std::string,int>> total_items(totals.begin(), totals.end());
    std::sort(total_items.begin(), total_items.end());
    std::cout << "Totals:\n";
    for (auto &p : total_items) std::cout << p.first << ' ' << p.second << '\n';
}

Notes/cautions: skip empty lines and malformed tokens; for very large files prefer streaming outputs (avoid storing excessive per-line history); use a regex if enforcing a strict ID format; be mindful of locale when using toupper if non-ASCII data may appear.

Recommended Answers

All 3 Replies

Perhaps something like this.

string line;
  while ( getline(file,line) ) {
    istringstream s(line);
    string word;
    while ( s >> word ) {
      cout << line << " ";
    }
    cout << endl;
  }

Hi Salem, thanks a lot!

I have just one doubt, how I will take just the columns AFTER id? (RR TT YY ...)

Thanks for your help

Thanks Salem! SOLVED! Cheers and have a nice weekend

string line;
  while ( getline(file,line) ) {
    istringstream s(line);
    string word;
    s >> word; // ADD
    while ( s >> word ) {
      cout << word << " ";
    }
    cout << endl;
  }
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.