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).
Abdullah Ejaz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.