Saving array contents to file using Visual Studio immediate window - visual-studio

I am using Visual Studio 2017 and C++ to debug a huge project
I don't use it frequently so I am not sure if this is possible or not, but is there any way to save all the contents of an array to a file using the immediate window? I have been looking around to find out the capabilities of the immediate window but there doesn't seem to be any place where this is clearly explained
This page comes up on google which only says it's possible to display contents of a variable, but doesn't give much more information
By looking around stackexchange I have found that a range of values in an array can be displayed using something like
array_name, 10
which will show the first 10 elements of an array, and this answer says there is a way to simply direct the output in the immediate window to a file, but my array has thousands of elements, and the array_name, XX trick only displays first 100 elements on the array in the immediate window
Is there a straightforward way to save contents of a variable or array to file in Visual Studio 2017 while the program is at a breakpoint?

You can get the content of any variable by copying it out of the watch window. From there you can pasted it into a file. I don't think there's anything to write it to a file from VS. You could of course have your program do that.
To get the variable values to the clipboard add a watch expression for your variable in the watch window (it's along the bottom of VS). However, don't just copy the expression (as that will get capped) expand it out then select all (Ctrl+A) and then right click Copy Value and you should get it to the clipboard. Depending on the contents of your array there might be truncation.

Related

Is there a way to properly dump the navigation history in Visual Studio?

Very frequently, I will be looking for a specific section of code where something happens, and will reach there by starting at a function at a high level of abstraction and go lower by successively opening the code of called methods. Eventually I will find what I'm looking for, and I would like to save the path that I took to get there - which is pretty much what the call stack would be if I had put a breakpoint in that code and stopped here at runtime, except that I'm just inspecting the code.
I'm aware the little arrow next to the "Back" arrow lets me somewhat get that in the UI and I can then take a screenshot of what I'm shown, but that's not a fantastic solution. The names of the functions are trimmed (leading to cases where it could match several functions), the line number is seldom shown (only if there was no code at that line), and I would much rather have the text format to begin with so I can copy the function names into a search tool rather than type them from the screenshot later.
So I was wondering: is there a way to dump the navigation history in Visual Studio ?
Where I could for ex. ask for the last 50 cursor positions, and get the file, file path, line number & possibly the code at that line in text format or some more intelligent thing, should the IDE support that.
Thanks.
PS: I found this very similar question Is there extension for viewing navigation history in Visual studio? that's >5 years old and didn't have a satisfying answer, so I'm trying my luck again, hoping things have changed since if there was no solution back then.

Change variable name in multiple lines in visual studio

So, I have some code for a Rect variable mageSection:
And I'd like to copy the same code for a different variable warriorSection. To achieve this:
So I'd like to know if there is a visual studio shortcut for allow you to change variable name in multiple line in visual studio without refactoring the whole variable name.i.e. I don't have to manually change those names from mageSection to warriorSectionfor these 4 lines.
It is a question about shortcut in visual studio rather than writing functions, as I'd like to know the shortcut for this. Many thanks!
Copy and paste the section of code.
Now select the pasted code and hit Ctrl-H to bring up the Quick Replace dialog.
In the top box, type "mageSection".
In the bottom box, type "warriorSection".
Hit Enter and it will find the first occurrence and replace it.
Hit Enter three more times...done.
If I'm understanding your question correctly you should be able to hold down alt while clicking into multiple lines and change them all at once. Then hit ESC to exit multiline editing.

Navigating within Visual Studio DataTip

When a DataTip is open while debugging in Visual Studio it appears the only way to get to a specific object property is scrolling with the mouse.
This can get very cumbersome as some objects can have hundreds of properties and scrolling to a property near the bottom takes a long time.
Yes, I know I can scroll faster if I hover the mouse over the bottom edge and pin a property once I found it but it can still take a long time to get to the property and pin it.
I've tried various combinations with Ctrl and Alt but what ever key I press the DataTip is promptly closed...
Once the DataTip has focus, you don't have to use the mouse. You can use the PgUp/PgDn (Page Up/Page Down) keys on your keyboard. But you're right: There is not a way to get to the end of the list or to get to items that start with a certain letter. When I need to do that often I take one of two approaches:
If possible, I create a [temporary] variable assignment after the code-line of interest and ensure that the variable gets assigned to the property of interest.
Create a watch variable and interact with that.
Open an Immediate Window and paste a copy of the object-instance name in question. Once in the Immediate Window, IntelliSense is available as soon as you type the "."--you can then type the letter of the property in question and drill-down as needed more quickly.
(Probaby the best tip!)
Open (and keep open for convenience) a Debug | Windows | Autos and/or Debug | Windows | Locals window. Those windows simplify navigating variables near the code break or in the current module--and they even seem to retain their tree-node-expanded state in some cases while debugging. More information is available here: https://msdn.microsoft.com/en-us/library/bhawk8xd.aspx
You could use the oz-code as an extension tool which could help you search the properties or variables easily.
Search in debug mode inside an object

Visual studio Paste without moving cursor to the end

Is it possible to paste some string in visual studio via some shortcut or other extension to keep the cursor where it is after the paste?
This way if I add multiple words of string I will not have to go with the cursor back to the location via ctrl-arrows or home key when I don't want the cursor to go to the end of pasted text.
I actually found a way to do it with resharper - if any better suggestions out there please feel free to answer.
Create a simple template as
$END$$CopiedContent$
and set the CopiedContent variable to clipboard content. Give it a shortcut vc (ctrl-v inverted) - and have fun. It will require an extra left arrow and escape char to get desired effect.
It costs VC[TAB][Left_ARROW][ESCAPE] meaning 5 characters which is more than I originally wanted. Willing to trade for a better answer.

Extending visual studio debugger

I have linked list containing few tens of objects like this:
struct Item {
Item * next;
const char * name;
....
};
When I want to see in debugger in visual studio what item list holds, I need to hover/click on next many times to expand whole list until I hit nullptr. This is slow, error-prone (hand slips and I can start all over again) and not very organized.
Is there any scripting for VS2015 debugger available in which I could iterate whole list and just dump the name into console or whatever?
EDIT: I found about concord extensibility api ( https://blogs.msdn.microsoft.com/visualstudioalm/2015/10/02/announcing-visual-studio-debug-engine-extensibility-samples/ ) but it seems rather complex.
This is supposed to be in-house tool, so speed/ease of development is more important than robustness and/or easy of deployment.
If the data set you are working with is small, I would go with the tried and true method of std::cout.
Or just dump the contents of the list into a file and put a break point after that file is written to so you can check its contents before the program continues.
You can do it. Insert a Tracepoint (Right mouse button> Breakpoint >Insert Tracepoint) and READ CAREFULLY to whole text of that window. Then you will know HOW to print to the Output window WHAT you want.
Insert also a breakpoint on another line that is CONDITIONAL. Just put a normal breakpoint, then over the red ball, Right mouse button > Condition, then input
!next
Notice the !

Resources