system() cant run my program because space [duplicate] - c++

This question already has answers here:
windows C system call with spaces in command
(1 answer)
C++ system() not working when there are spaces in two different parameters
(3 answers)
Closed 2 years ago.
i want run my program
so i do this program:
string zpath1, zpath2, zpath3, zpath3;
zpath1 = "C:\\Users\\";
zpath2 = zpath1 + getenv("USERNAME");
zpath3 = zpath2 + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\ConsoleApplication23.exe";
system(zpath3.c_str());
but i get a problem
'c:\User\adrian\AppData\Roaming\Microsoft\Windows\Start' is not knowing...
so i thing the problem is the space between "Start Menu"
how i can solve this problem ?
thank you

There are three possible solutions, one of two kinds of quotation marks or escape space character
zpath1 = "'C:\\Users\\";
// ^
zpath3 = zpath2 + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\ConsoleApplication23.exe'";
// ^
zpath1 = "\"C:\\Users\\";
// ^^
zpath3 = zpath2 + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\ConsoleApplication23.exe\"";
// ^^
zpath3 = zpath2 + "\\AppData\\Roaming\\Microsoft\\Windows\\Start^ Menu\\Programs\\Startup\\ConsoleApplication23.exe";
// ^
Thanks to #Eljay. Since the backlash \ is used in Windows as a path component separator, it can't be used as escape char, the carer symbol ^ is used as escape char in Windows command prompt.

Related

Replace text \n with actual new line. (C++) [duplicate]

This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}

Why when i add \ to cout it dosent display it on output [duplicate]

This question already has answers here:
What does a backslash in C++ mean?
(4 answers)
Closed 8 months ago.
When I try to do printf("----/");, the \ is removed from the output
#include <iostream>
int main() {
std::cout << ("Welcome to the game\n\n");
printf("--------\n");
printf("----O---\n");
printf("---/|\--\n");
printf("---/-\--\n");
}
output:
--------
----0---
---/|--
---/---
The \ character is a special one, it's the escape character. You can write special things with it (like \n for new line). Because this character is special, writing it to console requires using \\.
The answer has been given already: \ is special, because it's the escape character, and you need to escape it with itself if you want a literal \ in the outoupt.
This, however, can make the drawing confused.
To improve the things, you can use raw strings literals
printf(R"(--------
----O---
---/|\--
---/-\--)");
Notice that I've truly inserted a line break in place of the \n escape sequence, because \n would be literally interpreted as a \ followed by the letter n.
Clearly, you can still have issues, if a string contains exactly the characters )", because it will be detected as the closing token of the string:
// +---- code invalid from here
// |
// v
printf(R"(-----)"---)");
but you can still solve it, for instance by adding another set of wrapping "s:
printf(R""(-----)"---)"");
or anything else in between the " and (, and between ) and " (not symmetrized):
printf(R"xyz(-----)"---)xyz");
// ^
// |
// +--- not zyx

How to replace a string between two string using regex [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 4 years ago.
How to replace a string between two string in javascript
StartLine = `/*TESTSTART*/`;
Endline = `/*TESTEND*/`;
OriginalContent = `/*TESTSTART*/
testing
not
working
/*TESTEND*/`;
var e = OriginalContent .replace(/(StartLine)[\s\S]*?(Endline)/,' it's
working
fine');
OUTPUT = `/*TESTSTART*/
it's
working
fine
/*TESTEND*/`
1) How to check if the string contains / in regular exp?
2) if I stored sting in one variable, how can I use this variable in regular exp?
You can escape a / character with a backslash \ if you're using / to start your regular expression. But in this case, since you want to include the value of a variable in your regular expression, you should use a string to represent a regex instead, in which case there is no need to escape / but you should escape other special regex characters such as * with two backslashes, and you can simply concatenate the variable with the other string literals and variables to form the full regex:
StartLine = '/\\*TESTSTART\\*/';
Endline = '/\\*TESTEND\\*/';
...
var e = OriginalContent.replace(StartLine + '[\s\S]*?' + Endline, "it's
working
fine");

Remove Bracket [*TESTABC*] along with content from String prefix in java [duplicate]

This question already has answers here:
Java replace all square brackets in a string
(8 answers)
Closed 4 years ago.
I am having string as below and want prefix to be removed [*TESTABC*]
String a = "[*TESTABC*]test#test.com";
Expected result: test#test.com
I tried below code but it is removing the bracklets only. I need to remove the inner content also.
The string with come in this pattern only. Please help.
String s = "[*TESTABC*]test#test.com";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s); //*TESTABC*test#test.com
int index;
String s = "[*TESTABC*]test#test.com";
index=s.indexOf(']');
s=s.substring(index+1);
System.out.println(s);
**i solve your question according to your problem and output **

Python lstrip Sometimes Removes Extra Character [duplicate]

This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Closed 7 years ago.
I have Python 2.7 code that operates on a list of files. In part of the code I strip away the directory information. Today I was surprised to find that code didn't work correctly, when the file names begin with "s". This sample code demonstrates the problem:
import os
TEST_RESULTS_DIR = ".." + os.sep + "Test Results"
filename = TEST_RESULTS_DIR + os.sep + "p_file.txt"
stripped_filename = filename.lstrip(TEST_RESULTS_DIR + os.sep)
print ("%s : %s") % (filename, stripped_filename)
filename = TEST_RESULTS_DIR + os.sep + "s_file.txt"
stripped_filename = filename.lstrip(TEST_RESULTS_DIR + os.sep)
print ("%s : %s") % (filename, stripped_filename)
When I run this code, I get this:
..\Test Results\p_file.txt : p_file.txt
..\Test Results\s_file.txt : _file.txt
Does anyone understand why?
Lstrip doesn't replace a string at the beginning of another string, it strips all characters that match the characters in the string argument from the string it is called on.
For example:
"aaabbbc".lstrip("ba") = "c"
Your directory has an s in it, so it get's striped, you would see the same result if the file started with a u or an e.