i want to build program that recive (c code in txt file) then searching for words (int , double, char, float) and count number of vaiables appear according to that. then store the variables in array (e.g. int x; i want to store X in array and so on). i build code that search of word e.g (int) in file and count number of its appearance, but still i dont know how to extract the word after the searched word. (e.g X,Y..etc)

Dani AI

Generated

— the stream-extraction approach in the thread finds the token after the type but leaves punctuation (like x;) and can't handle comma-separated declarations, initializers, pointers or arrays. asked for code; below is a short, practical two-pass pattern that solves the common cases without rewriting a compiler.

First pass: read the file into a vector of lines and use a regex to find declarations of the simple types (int, double, char, float). Capture the comma-separated variable list, split it, then "clean" each item by removing initializers (= ...), pointer/ref markers (*/&) and array brackets ([...]) and store the variable name plus its declaration line index.

Second pass: for each collected variable scan the lines from the end toward the top and use a whole-word regex (e.g. "\bname\b") to find the last line that contains that identifier (optionally skip the declaration line). The first match encountered is the last usage; store that full line.

Example (concise C++ sketch):

// read all lines
std::vector<std::string> lines = read_all_lines("file.c");

// decl regex: captures the comma list after the type
std::regex decl(R"(\b(?:unsigned\s+)?(?:int|double|char|float)\b\s*([^;]+);)");

// extract names, clean them (remove =, *, &, [..])
// then for each name build word-pattern "\\b" + name + "\\b"
// scan lines from bottom up, skip declaration index, first regex match is last occurrence

Cautions and tips: strip comments and string literals before regexes (they create false positives). This approach works for simple, well-formed C code but will fail on edge cases: typedefs, macros, function parameters, class/struct members, templates or declarations that span multiple lines. For robust, production-grade analysis use a real parser/tokenizer (libclang/Clang AST, Tree-sitter, or a C lexer).

Recommended Answers

All 2 Replies

Show the code you have now and we should be able to help you modify it.

this is the code.. the code recivices file and earch for a word in a file if found it store the line in array.. what i want to do, is searching for a word if found i need to store the word next to the searhed word. e.g. if text file inclue the following text ( int x; int y; cout<<"hi";x=5; x=y+z;) when i searched for int word both x,y stored in array. then after that search for the last place where x and y appeared and store x=y+z. how i can do so??

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


int main()
{
    std::cout  << "Write the path of the file\n" ;
    std::string path ;
    std::cin >> path ;
    std::string line;
    std:: string names[50];
    std:: string output;
    std::ifstream file( path.c_str() ) ;

    if( file.is_open() )
    {
        std::cout << "File '" << path << "' opened.\n" ;

        std::cout << "Write the word you're searching for\n" ;
        std::string word ;
        std::cin >> word ;

        int countwords = 0 ;
        std::string candidate ;
        int i=0;
        while( file >> candidate ) // for each candidate word read from the file 
        {
        //  std::cout<< candidate<<"\n";
            if( word == candidate ) 
            { ++countwords ;
             file >> output;
             //getline (file,line);
             names[i]=output;
             i++;
            //  std::cout<<line;

            }
        }
           std:: cout <<"input size="<< (countwords * 32);
        std::cout << "The word '" << word << "' has been found " << countwords << " times.\n" ;
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.