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 3597102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:04:30+00:00 2026-05-18T20:04:30+00:00

I have tested the encryption part of the program and it works just fine

  • 0

I have tested the encryption part of the program and it works just fine for individual files, or even things like “file1,file2,file3” but it doesn’t work with directories. The code looks fine to me, however when executing it gives me a segmentation fault.

It’s supposed to encrypt files in directory and write them in the same directory with a new extension (old name + “.wbf”) and decryption with extension removal upon the opposite. I’m only going to post the parts of code that deal with the files, the do_crypt function that works with individual files works just fine, and I think it’s not the source of my problems.

    // PHP explode function

    vector<string> explode (string text, char separator)

    {

        vector<string> split;

        int last_trip = 0, pos = 0;

        text += separator;

        for (pos = 0; pos < text.size(); pos++)

        {

            if (text[pos] != separator)

            {

                // continue with iteration

            }

            else

            {

                split.push_back(text.substr(last_trip, pos - last_trip));

                last_trip = pos + 1;

            }

        }

        return split;

    };

    // LINUX -- directory listing function
    string LS (string dir)
    {
        DIR *dp;
        vector<string> files;
        struct dirent *dirp;
        if ((dp = opendir(dir.c_str())) = NULL)
        {
            cout << "Error (" << errno << ") opening " << dir << endl;
            //return errno;
        }
        while ((dirp = readdir(dp)) != NULL)
        {
            files.push_back(string(dirp->d_name));
        }
        closedir(dp);
        string explosive = "";
        for (int i = 0; i < files.size(); i++)
        {
            explosive += files[i];
            if (i != (files.size() - 1)) { explosive += ','; } 
        }
        return 0;
    }

// various functions for encryption

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

{

    cout << "\t! ENCRYPTR -- File encryption utility written by WBlinder, 2010. !" << endl << endl;

    cout << "\t\t\t\tOPTIONS:" << endl;

    cout << "\t\t\t1\tCRYPT A FILE" << endl << "\t\t\t2\tDECRYPT A FILE" << endl << endl;

    cout << "choice > ";

    int opt;

    cin >> opt;

    string sin, sout;

    string dummy; getline(cin, dummy);

    /*cout << "input files > ";

    cout.clear(); cin.clear();

    getline(cin, sin);

    vector<string> fin = explode(sin, ',');

    cout << "output files > ";

    getline(cin, sout);

    vector <string> fout = explode(sout, ',');

    if (sin.size() != sout.size())

    {

        cout << "NO. INPUT FILES UNEQUAL NO. OUTPUT FILES" << endl;

        return 1;

    }*/
    string dir;
    cout << "dir > "; 
    getline (cin, dir);
    vector<string> input = explode(dir, ',');
    vector<string> output = input;
    switch (opt)
    {
        case 1:
            for (int i = 0; i < output.size(); i++)
            {
                output[i] = output[i] + ".wbf";
            }
            break;
        case 2:
            for (int i = 0; i < output.size(); i++)
            {
                output[i] = output[i].substr(0, (output[i].size() - 4));
            }
            break;
    }

    cout << "password > ";

    getline(cin, key);

    cout << "N > ";

    cin >> N;

    cout << "(768 => fairly secure\t3072 => secure)\nextra rounds > ";

    cin >> drop;

    for (int brick = 0; brick < input.size(); brick++)

    {

        do_crypt(opt, dir + input[brick], dir + output[brick]);

    }

    /*string text;

    cout << "text to split: ";

    getline (cin, text);

    vector<string> tnt = explode(text, '.');

    for (int i = 0; i < tnt.size(); i++)

    {

        cout << i << ": " << tnt[i] << endl;

    }*/

}
  • 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-05-18T20:04:31+00:00Added an answer on May 18, 2026 at 8:04 pm

    There is many problem in your LS function. First, you should make it directly return a vector<string> instead of packing the data in a string using a comma to separate value. This would save you a call to explode function, and would not break if the directory contains a filename with a comma in it (which is a valid name on Linux).

    But the bigger problem (that is causing) your segfault is the return 0 line. Since your function is declared to return a string object, and since string class has an implicit constructor from const char*, this is interpreted by the compiler as return string(NULL). And when called with a NULL pointer this constructor raise a logic_error exception. As you didn’t catch the exception, the C++ runtime call the abort function. This function cause a segfault by design in order to stop execution (and if enabled generate a coredump to allow post-morten debugging).

    You should at least rewritte your LS function like that:

    string LS (string dir)
    {
        DIR *dp;
        vector<string> files;
        struct dirent *dirp;
        if ((dp = opendir(dir.c_str())) == NULL)
        {
            cout << "Error (" << errno << ") opening " << dir << endl;
            return string();
        }
        while ((dirp = readdir(dp)) != NULL)
        {
            files.push_back(string(dirp->d_name));
        }
        closedir(dp);
        string explosive = "";
        for (int i = 0; i < files.size(); i++)
        {
            explosive += files[i];
            if (i != (files.size() - 1)) { explosive += ','; } 
        }
        return explosive;
    }
    

    Or better, change its signature to return a vector<string> and rewrite it like that:

    vector<string> LS (string dir)
    {
        DIR *dp;
        vector<string> files;
        struct dirent *dirp;
        if ((dp = opendir(dir.c_str())) != NULL)
        {
            while ((dirp = readdir(dp)) != NULL)
            {
                files.push_back(string(dirp->d_name));
            }
            closedir(dp);
        }
        else
        {
            cout << "Error (" << errno << ") opening " << dir << endl;
        }
        return files;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tested the encryption part of the program and it works just fine
My MySQL database can store the euro symbol just fine (as I have tested
I have tested this on Firefox, Opera and Seamonkey. It works fine. When it
I have a script that works fine in all browsers that I have tested
I have tested my website on all browsers and it works fine: http://bgsorken.com/sandbox/index.php Besides
I have tested this servlet and it works well, except in Google Chrome it
I have tested a couple of benchmarking snippets on Delphi like this one: uses
I have a class with a method that works and I have tested it
I have the following code that I have tested and works: using (new Impersonator(Administrator,
I have just deployed my first JSF site using eatj.com. I have tested my

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.