Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:57+00:00 2026-06-02T03:38:57+00:00

I have to write a program that will go through a given folder and

  • 0

I have to write a program that will go through a given folder and use regex_search to find every instance of a certain string. I’ve got the regex_search working on it’s own now, and I’m just trying to figure out how to go through each file. I want to attempt it using directory but am unsure where I would put it. Would I have to put the search through the file into my main method or would I have to create a seperate function outside of the main method for going through each file and call on it within the main method?

This is what I have now. Any tips you guys can give on how to approach this would be greatly appreciated!

Right now the function of it is to read an input text file and output a txt file that shows all the instances and the line number of each apperance. I am not required to see which lines they are on, use a particular file, or make an output file for this program, what I find will simply be printed to the console. I’ve left what I have now because I’m not sure if I’ll checking each indivdual file in a similar fashion just with a different cariable name.

#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <vector>
#include <regex>
#include <iomanip>

using namespace std;

int main (int argc, char* argv[]){

// validate the command line info
if( argc < 2 ) {
    cout << "Error: Incorrect number of command line arguments\n"
            "Usage: grep\n";
    return EXIT_FAILURE;
}

//Declare the arguments of the array
    string resultSwitch = argv[1]; 
string stringToGrep = argv[2];
string folderName = argv [3];
regex reg(stringToGrep);


// Validate that the file is there and open it
ifstream infile( inputFileName );
if( !infile ) {
    cout << "Error: failed to open <" << inputFileName << ">\n"
            "Check filename, path, or it doesn't exist.\n";
    return EXIT_FAILURE;
}



while(getline(infile,currentLine))
{
    lines.push_back( currentLine ); 
            currentLineNum++;
            if( regex_search( currentLine, reg ) )
                    outFile << "Line " << currentLineNum << ": " << currentLine << endl;



}

    infile.close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:38:59+00:00Added an answer on June 2, 2026 at 3:38 am

    Reading a directory/folder is operating system dependent. In a UNIX/Linux/MacOS world, you use opendir(), and readdir():

    #include <sys/types.h>
    #include <dirent.h>
    

    …

    DIR *directory = opendir( directoryName );
    
    if( directory == NULL )
        {
        perror( directoryName );
        exit( -2 );
        }
    // Read the directory, and pull in every file that doesn't start with '.'
    
    struct dirent *entry;
    while( NULL != ( entry = readdir(directory) ) )
    {
    // by convention, UNIX files beginning with '.' are invisible.
    // and . and .. are special anyway.
        if( entry->d_name[0] != '.'  )
            {
            // you now have a filename in entry->d_name;
            // do something with it.
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a program that will, when executed, will go through
I have to write a program using the IAS instruction set that will loop
I have to write a program that sniffs network packets (part1-the simple part). And
I have to write a daemon program that constantly runs in the background and
I wanted to write a program that test if two files are duplicates (have
I have been assigned wit the task to write a program that takes a
I have a program that looks like this. I need to consistently write something
I have a robot that I need to write an autonomous program for. The
I have to write a program in python where the user is given a
I have to write a program to check if any random string is present

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.