Hello, it's been a while since I took my last programming class, and I must say I don't know how to do the following. I want to create a program that pulls data from a txt file and then lists that data. The data is simply C++ variable names, reserved words etc.The program should be able to pull any information from that text file. Just like this one:

class A {
int a[11],x,y,z;
char *oneString;
public:
A() { oneString= new char[100]; }
A() { delete oneString; }
void f();
};
void A::f() {
int temp;
x=y+1;
z=x+2; z= x*x;
}

It pulls the data and displays it.

Int
char
class


etc.

The input is just a regular text file where each line is terminated with an end-of-line character. I honestly have no idea how to do this. I thought about using arrays to get the info and display it. But would that array automatically process the info contained in the text file, since it's not just a list but classes that are being used in the input file?

Dani AI

Generated

For : 's link is the right starting point — open the file and read it into memory or line-by-line. After that the job is parsing, not magically done by an array: remove/skip comments and string/char literals, then extract tokens that match the C++ identifier pattern (letters or underscore followed by letters/digits/underscore). Push those tokens into a container (std::vector for order, std::unordered_set for uniqueness, std::unordered_map to count occurrences), and compare them against a set of reserved keywords to list language words like int, char, class, etc.

A compact extractor (skips // and /*...*/ comments and literal content, then finds identifiers):

#include <fstream>
#include <string>
#include <vector>
#include <cctype>
#include <iterator>

std::vector<std::string> extract_identifiers(const std::string &text) {
    std::vector<std::string> ids;
    bool in_line = false, in_block = false, in_dq = false, in_sq = false;
    size_t i = 0, n = text.size();
    while (i < n) {
        if (in_block) {
            if (i+1<n && text[i]=='*' && text[i+1]=='/') { in_block = false; i += 2; continue; }
            ++i; continue;
        }
        if (in_line) {
            if (text[i]=='\n') in_line = false;
            ++i; continue;
        }
        if (!in_dq && !in_sq) {
            if (i+1<n && text[i]=='/' && text[i+1]=='/') { in_line = true; i += 2; continue; }
            if (i+1<n && text[i]=='/' && text[i+1]=='*') { in_block = true; i += 2; continue; }
        }
        if (!in_sq && text[i]=='"') { in_dq = true; ++i; continue; }
        if (in_dq) { if (text[i]=='\\' && i+1<n) { i+=2; continue; } if (text[i]=='"') { in_dq=false; ++i; continue; } ++i; continue; }
        if (!in_dq && text[i]=='\'') { in_sq = true; ++i; continue; }
        if (in_sq) { if (text[i]=='\\' && i+1<n) { i+=2; continue; } if (text[i]=='\'') { in_sq=false; ++i; continue; } ++i; continue; }

        if (std::isalpha((unsigned char)text[i]) || text[i]=='_') {
            size_t j = i+1;
            while (j<n && (std::isalnum((unsigned char)text[j]) || text[j]=='_')) ++j;
            ids.emplace_back(text.substr(i, j-i));
            i = j;
            continue;
        }
        ++i;
    }
    return ids;
}

// usage:
// std::ifstream in("input.cpp");
// std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
// auto tokens = extract_identifiers(content);

Filter and count tokens with a keyword set and a map:

#include <unordered_set>
#include <unordered_map>
#include <iostream>

std::unordered_set<std::string> keywords = {"int","char","class","void","for","if","else","return","new","delete"};
std::unordered_map<std::string,int> counts;
for (const auto &t : tokens) if (keywords.count(t)) ++counts[t];
for (const auto &p : counts) std::cout << p.first << " : " << p.second << '\n';

Notes and pitfalls: naive tokenization will catch identifiers inside strings/comments unless skipped; templates, scope (::), destructors (~A), and macros complicate parsing; regexes are convenient but fragile for C++ grammar. For production-quality results use a real parser (libclang/Clang tooling) or a full lexer if full accuracy is required. Arrays do not “process” the file — they are storage for whatever parsing code inserts into them.

Recommended Answers

All 2 Replies

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.