Im trying to read a file in cpp/clr and perform calculations on it and display it using GUI but Im getting errors Im not familair with

I am opening the file in binary mode and trying to get the first digit (number_of_poly) in the first row, the total number of rows, and the second column digit (number_of_vars) in the second row, the total number of columns, into an array of size number_of_vars (buff). I’m adding them to and storing them at the same index in another array of the same size (sum). I am getting the buffer overrun error at this point, reading to buff. I assume it’s because there are 10 rows (number_of_poly) but my array is of 5 (number_of_vars). While adding I get invalid data from ‘this->buff’ error.

String^ path;
ifstream reader;
openFileDialog1->ShowDialog();
path = openFileDialog1->FileName;
pin_ptr<const wchar_t> wch = PtrToStringChars(path);
int len = path->Length;
wchar_t* wchPtr = const_cast<wchar_t*>(wch);
wstring convertedString(wchPtr, len);

// Convert wstring to std::string
string nativepath(convertedString.begin(), convertedString.end());

// Now nativepath contains the converted string
reader.open(nativepath, ios::binary); // Open the file in binary mode

if (!reader.is_open()) {
    MessageBox::Show("Error opening file");
    return;
}
int temp_poly = 0, temp_vars = 0;
reader.read(reinterpret_cast<char*>(&temp_poly), sizeof(int));
reader.read(reinterpret_cast<char*>(&temp_vars), sizeof(int));

number_of_poly = temp_poly;
number_of_vars = temp_vars;

// Allocate memory for buff and sum arrays
buff = new int[number_of_vars];
for (int i = 0; i < number_of_vars; i++) 
{
    sum[i] = 0; // Initialize sum array elements to zero
}

MessageBox::Show(number_of_poly.ToString());
MessageBox::Show(number_of_vars.ToString());
for (int i = 0; i < number_of_poly; i++)
{
    for (int j = 0; j < number_of_vars; j++) 
    {
        int value;
        if (!(reader.read(reinterpret_cast<char*>(&value), sizeof(int)))) 
        {
            MessageBox::Show("Error reading file");
            return;
        }
        buff[j] = value; // Store the read value in buff array
        sum[j] += buff[j]; // Update sum with the read value
        String^ intString = Convert::ToString(value); // Convert int to String
        richTextBox1->AppendText(intString + "n"); // Append with a newline
    }
}

reader.close(); // Close the file after reading
buff[j] = value; // Store the read value in buff array
sum[j] += buff[j]; // Update sum with the read value

I expected that the value in the txt file that I opened in binary mode to be read into the buff array and then it to be added but instead I get

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at CppCLRWinFormsProject.Form1.button1_Click(Object sender, EventArgs e) in D:C++_practice_problemsAssignmentsAssignment_06Form1.h:line 151
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9232.0 built by: NET481REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Assignment_06
    Assembly Version: 1.0.8883.5827
    Win32 Version: 
    CodeBase: file:///D:/C++_practice_problems/Assignments/Assignment_06/x64/Debug/Assignment_06.exe
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9236.0 built by: NET481REL1LAST_B
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9181.0 built by: NET481REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9032.0 built by: NET481REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9032.0 built by: NET481REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9236.0 built by: NET481REL1LAST_B
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9032.0 built by: NET481REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Accessibility
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.9032.0 built by: NET481REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

when I try to read the file.

For context below are the contents of the file:

10 -----> number_of_poly
5  -----> number_of_vars
0   0   0   1   0   
1   0   0   1   0   
0   0   0   0   0   
1   0   0   0   0   
0   0   0   0   0   
0   0   0   0   0   
0   0   0   0   0   
0   0   0   0   0   
0   0   0   0   1   
0   1   0   0   0   

The text is added for clarity purposes. It is not present in the actual file (—–> text).

New contributor

Abdullah Ejaz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật