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)

2 Years
Discussion Span
Last Post by rasha80

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);
            //  std::cout<<line;

           std:: cout <<"input size="<< (countwords * 32);
        std::cout << "The word '" << word << "' has been found " << countwords << " times.\n" ;
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.