How to search for a specific text file in Winforms C++ using a textbox and a Browse button

I am currently working on a project which can compress a text file using the Huffman Coding Algorithm in CLR C++.

I am facing a problem with regards to searching a specific file in a directory. I have a GUI which allows me to enter the name of the file and then click the Browse button. Picture of my GUI before entering any data Now, when I enter the filename and click the Browse button, the following message is displayed: Message displayed after entering the filename and clicking browse

This is a problem because the file is actually present in the directory. File in the directory

I want to know, how can I get the program to read my file? Following is the code in the header file:

#include "MyForm.h"
#include<iostream>
#include<fstream>
#include<stdio.h>
#include "dirent.h"
#include <msclr/marshal_cppstd.h>

using namespace msclr::interop;
using namespace System;
using namespace std;


class HuffmanCoding
{
    if ((directory = opendir("C:\UserstaimosourcereposHuffmanHuffman")) != NULL)
        {
            while ((x = readdir(directory)) != NULL) {

                {

                    if ((cPath) == (x->d_name))
                    {
                        result = true;  //if file found then  assign  result to false.

                        break;     // break the loop if file found.

                    }
                }
            }
            
        }
        closedir(directory);
        if (result == true)
        {
            
            label2->Text = "File found successfully!";
        }
        else if (result==false)
        {
            label2->Text = "File not found. Please enter filename again.";
        }

};

The code in Form.h is as given below:

#include "Header.h"
#include "dirent.h"
#include<iostream>
#include<string>
#include <msclr/marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
using namespace std;



HuffmanCoding h;



namespace Huffman{

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^ button1;
    private: System::Windows::Forms::Button^ button2;
    private: System::Windows::Forms::Button^ button3;
    private: System::Windows::Forms::TextBox^ textBox1;
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::Label^ label2;
    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(12, 178);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"Compress";
            this->button1->UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(186, 178);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(75, 23);
            this->button2->TabIndex = 1;
            this->button2->Text = L"Decompress";
            this->button2->UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this->button3->Location = System::Drawing::Point(199, 120);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(75, 23);
            this->button3->TabIndex = 2;
            this->button3->Text = L"Browse";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(39, 120);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(154, 20);
            this->textBox1->TabIndex = 3;
            this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->label1->Location = System::Drawing::Point(34, 38);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(213, 25);
            this->label1->TabIndex = 4;
            this->label1->Text = L"FILE COMPRESSOR";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
                static_cast<System::Byte>(0)));
            this->label2->Location = System::Drawing::Point(36, 149);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(0, 13);
            this->label2->TabIndex = 5;
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 261);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = L"MyForm";
            this->Text = L"MyForm";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) 
    {

    }
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) 
{
    
    h.SearchAndReadFile(textBox1,label2);
}
};


}

Leave a Comment