- #1
yungman
- 5,755
- 293
Hi
I am running debug on a program with reinterpret_cast, I ran into funny display and I use debug to look at it step by step. Here is the program:
This program is to proof reinterpret_cast<char*>(Iw), that I can use it as char pointer to write text into a text file and should show 1,2,3,4,5 in the test1.txt. That I can read it back in test >> Ar; and give {1,2,3,4,5}.
You can see below when the program break at line 30, Ar contain "\x1\x2\x3\x4\x5" Obviously I got the 1, 2, 3, 4, 5 written into the test1.txt. But what is \x?
This is the content of "test1.txt". They are not characters 1,2,3,4,5 that is supposed to be written.
When I complete running the program, this is what is displayed, obviously it's not what's in the file.
My issue with the debugger is I cannot display what's being written in the file while it is running. Obvious, the index = 5 meaning while loop in line 15 was run 5 times.(incremented to 5 on the last loop). Is there any way to display what is written into the file step by step.
I don't know what I did wrong in the program and I don't know how to make the debugger help me more where it went wrong.
Thanks
I am running debug on a program with reinterpret_cast, I ran into funny display and I use debug to look at it step by step. Here is the program:
C++:
//Experiment reinterpret_cast
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int index = 0;
char Tw[] = " This is a test";
int Iw[] = { 1, 2, 3, 4, 5 };
int Ir[20] = { '\0' };
fstream test;
test.open("test1.txt", ios::out);
while (index < (sizeof(Iw)/sizeof(Iw[0])))
{
test << reinterpret_cast<char*>(&Iw[index]);
index++;
}
test.close();
test.open("test1.txt", ios::in);
char Ar[50] = { NULL };
int ind = 0;
test >> Ar;
cout << "{";
while (Ar[ind] != NULL)
{
cout << Ar[ind];
ind++;
}
cout << "}\n\n";
test.close();
return 0;
}
You can see below when the program break at line 30, Ar contain "\x1\x2\x3\x4\x5" Obviously I got the 1, 2, 3, 4, 5 written into the test1.txt. But what is \x?
My issue with the debugger is I cannot display what's being written in the file while it is running. Obvious, the index = 5 meaning while loop in line 15 was run 5 times.(incremented to 5 on the last loop). Is there any way to display what is written into the file step by step.
I don't know what I did wrong in the program and I don't know how to make the debugger help me more where it went wrong.
Thanks
Last edited: