How do I open a file within notepad++ with C++? - c++

I'm trying to get my c++ program to open an sql file in notepad++. I can get it to open with notepad like this:
system("notepad.exe script_foo.sql");
But that's undesirable as it's not formatted. When I try to substitute notepad.exe for notepad++.exe like this:
system("'C:\Program Files\Notepad++\notepad++.exe' script_foo.sql");
I get a invalid syntax error.
Any issues where I'm going wrong?

The WinNT shell uses double-quotes to include spaces in a file name. Single quotes are not recognized. So you need
"C:\Program Files\Notepad++\notepad++.exe" script_foo.sql
as your command.
To embed this in C++ source code, you'll need to escape backslashes (as Andre already mentioned) and also the double-quotes.
system("\"C:\\Program Files\\Notepad++\\notepad++.exe\" script_foo.sql");

In C++, the backslash character \ is an escape character in strings. You need to double the backslashes to achieve what you really want:
system("'C:\\Program Files\\Notepad++\\notepad++.exe' script_foo.sql");

Related

How to search for line breaks within XML text (within tags)?

I've massive XML-file with text blocks and many of them contain unencoded line-breaks.
How to search line-breaks (/n) within XML text (within tags) and replace it with HTML-encoded line-breaks like
?
My code so far:
#include <regex>
...
std::string sInput_xml;
std::ifstream in(sFilePath_XMLFile);
// read file into input_xml
while(getline(in, sLine))
sInput_xml += sLine;
std::regex rxSearch("\>.*(\n)+.*\</");
std::regex_replace (sInput_xml, rxSearch,"
");
... and then I'd like to pass the string to the rapid-xml parser. Unencoded line-breaks are ignored by this (and many other) parser and I tried to replace it manually with
. It works perfectly but the file is 31k lines, it would take forever.
I'm not even sure if this regex is correct but my the VS compiler complains about the search_replace function not taking three params. But the should be a 3 param version like in the example on cplusplus regex replace.
Using RapidXML 1.13, an XML file with unescaped newlines in elements and attributes is parsed successfully, and the attribute and element values preserve the whitespace for me, so I think the search and replace is unnecessary.
Note that if you're debugging in Visual studio, the newlines are omitted from the tooltip when you hover over a variable in the editor, maybe that's what led you to believe they weren't preserved.
Regarding your problem with the regex_replace function, if you use an std::string for the third parameter it will compile. This seems to be a problem in Visual Studio 2010, as the const char* is accepted in Visual Studio 2013.
You will also need to be aware of characters to be escaped in both the search and replace strings if you still want to go down the regex route.
UPDATE: Now I realize that was representative code of how you were loading the file before introducing the regex, you should be aware that getline() does not include the newline, so it is your loading code which is removing the newlines from the file. Simplest thing would just be to use RapidXML to do the file loading directly:
#include "rapidxml_utils.hpp"
// ...
rapidxml::file<> xmlFile("test.xml");
rapidxml::xml_document<> doc;
doc.parse<0>(xmlFile.data());
Is there a reason for using c++ ?
maybe you can try sed
sed -i ':a;N;$!ba;s/\n/
/g' input.xml
the -i flag edit file in place, so make sure you have a backup before you run that.
reference
How can I replace a newline (\n) using sed?

Sublime Text macro to find and replace file path characters on current line

I use Sublime 2 for developing R and PHP code, although I imagine this shortcut would be useful for other languages.
If I copy the path of a file from Windows Explorer / XYPlorer (or other source) it has backslashes for directories. When entering a path into the source code, it needs forward slashes.
Sublime has some reasonably powerful macro commands, but I cannot think of a combination that would be able to:
take the string of text on the current line
replace all instances of '\' and replace them with '/'
Here is the workflow that I envisage:
Locate my filename in Explorer and copy its path
In Sublime, write a line of code and paste in the path
Hit a keyboard shortcut, say Ctrl+Shift+\, and all back slashes are converted to forward slashes
The result:
myPath = "E:\WORK\Code\myFile.csv";
Becomes:
myPath = "E:/WORK/Code/myFile.csv";
Without running the risk of backslashes elsewhere in the file being changed (e.g. \n characters), and without having to use multiple key presses or mouse clicks.
I imagine this would be possible with Regex. Two things I am no expert in are Sublime macros or regex, so I wonder if anyone else knows the magical commands that would achieve this?
I tried this for about 15 minutes. A few things:
Sublime text 2 doesn't allow for find/replace with macros
Sublime text 3 doesn't allow for 'find in selection'
So, I think you are kind of beat right now other than writing a plugin, which would be fairly straightforward.
This works for Sublime Text 3:
Type r before the string to tell python to read the directory as a raw string.
This way all the backslashes are read as slashes instead of 'ignore next character' (default meaning of \ in python)
Example
myPath = r"E:\WORK\Code\myFile.csv"
Python should now read the \ as /

executing filenames with spaces in cmd pmt Passed from c++ program

I am currently working on getting my program to execute a program (such as power point) and then beside it the path to the file I want to open. My program is getting the file's path by using:
dirIter2->path()
I get the 2 paths of the program and file, Merge them as one string and pass them into the following:
system(PathTotal.c_str())
this is working great but my only issue is that when the file name has a space in its name command prompt says it cannont find the file (becuase it thinks the file name ends when it gets to the first space. I have tried to wrap it with quotes but it is the acutal file name that need to be wrapped.
(eg. i have tried "C:\users\bob\john is cool" but it needs to be like this: C:\users\bob\"john is cool")
Does anyone have any suggestions on how I could fix this? I was thinking about getting the path to the folder to where the file and then getting the file name. I would wrap the file name with quotes then add it to the folder's path. I have tried using the ->path() like above but the only problem is that it only goes to outside of the folder's directory?
Is there a boost command that could get the enitre path to the file without getting the file aswell?
I am not commited to this idea if anyone has any better suggestions
Thanks
In both C and C++, the '\' is an escape character. For certain things (like '\n' or '\t') it inserts a control code; otherwise, it just gives you the next character.
So if you do something like:
fopen("C:\users\bob\john is cool", "r");
it's going to try to open a file named
C:usersbobjohn is cool
If you want those '\' characters in the output, you have to escape them. So you'd want:
fopen("C:\\users\\bob\\john is cool", "r");
On Windows with Visual Studio, I've also successfully used Unix-style separators:
fopen("C:/users/bob/john is cool", "r");
And in fact, you can mix them up:
fopen("C:/users\\bob/john is cool", "r");
I'm not familiar with C string operations, but couldn't you do the following rather easily?
int i = path.lastIndexOf("\\"); //Find the index of the last "\"
String quotedPath = path.substring(0, i+1); //Get the path up until the last "\"
quotedPath += "\"" + path.substring(i+2) + "\""; //Add quotes and concatenate the filename
Sorry for the Java, its the closest thing that I'm familiar with. I've made this a community wiki in case someone can edit the code to the equivalent C.
I'd also like to add that sometimes it is necessary to escape spaces as in the following:
cmd.exe -C C:/Program\ Files/Application\ Folder/Executable\ with\ spaces.exe
or
cmd.exe -C C:\\Program\ Files\\Application\ Folder\\Executable\ with\ spaces.exe

C++ on Windows: executable path with whitespace in system() call

I am trying to execute a file with parameters using the "system()" function in C++ on Windows, and it works as long as there are no whitespaces in the filename. For parameters, putting double quotes around the string works, but when I try the same with the executable itself, I get the following error:
"the filename,directory name, or volume label syntax is incorrect"
Does anyone know how to handle this correctly?
Use a string like this:
cmd /S /C "your entire command line string"
See: How do I deal with quote characters when using cmd.exe
It should work, look for the problem elsewhere.
Perhaps something in your flow is removing the whitespace or the double quotes from the string.

Devenv.exe with /I and whitespacess

I've tried to compile an application with Directx. But this causes an PRJ0030 error for $().
How can I escape critical characters like (,) or blanks. Refering to the cmd I've used ^ but it does not help.
AdditionalLibraryDirectories=""$(DIRECTX_ROOT)\Lib\x86""
&quot should not be there. $(DIRECTX_ROOT) requires the macro to be set in a project property sheet. You are better off spelling it out:
AdditionalLibraryDirectories="c:\blah\dx9\Lib\x86"
cmd.exe unfortunately isn't a proper shell like bash, and parsing the command line is up to each individual program. I can't speak for devenv.exe but a common convention is to surround troublesome strings with double quotes (").