C++ regex_replace not replacing string - c++

I'm a newbie with C++. I'm trying to learn string replacement.
I'm writing a short (supposed to be a tutorial) program to change directory names.
For example, I want to change "/home" at the beginning of a directory name with "/illumina/runs" thus:
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include<iostream>
#include <string>
#include <regex>
#include <iterator>
using namespace std;
std::string get_current_dir() {
char buff[FILENAME_MAX]; //create string buffer to hold path
GetCurrentDir( buff, FILENAME_MAX );
string current_working_dir(buff);
return current_working_dir;
}
int main() {
string b2_dir = get_current_dir();
std::regex_replace(b2_dir, std::regex("^/home"), "/illumina/runs");
cout << b2_dir << endl;
return 0;
}
I'm following an example from http://www.cplusplus.com/reference/regex/regex_replace/ but I don't see why this isn't changing.
In case what I've written doesn't make sense, the Perl equivalent is $dir =~ s/^\/home/\/illumina\/runs/ if that helps to understand the problem better
Why isn't this code altering the string as I tell it to? How can I get C++ to alter the string?

std::regex_replace does not modify its input argument. It produces a new std::string as a return value. See e.g. here: https://en.cppreference.com/w/cpp/regex/regex_replace (your call uses overload number 4).
This should fix your problem:
b2_dir = std::regex_replace(b2_dir, std::regex("^/home"), "/illumina/runs");

Related

All strings are unidentified

#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.

c++ Unable to output unicode characters even though I can write them directly

So thing is I can copy paste unicode characters like chess pieces directly to terminal( I'm using debian jessie linux) but whenever I write c++ code to do that, I get these � instead
here is my code
enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
setlocale(LC_ALL,"");
wchar_t piece='♗';
wcout<<piece;
}
I tried to use the hex or decimal code of the characters but it does not work
I also use vim to edit and it does show the characters while I'm typing.
There's no specification of what encoding should be used for wchar_t. I need to use mbstowcs function to convert that character. Like this, for example:
#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
setlocale(LC_ALL, "");
wchar_t piece;
mbstowcs(&piece, "♗", 1);
wcout << piece << endl;
return 0;
}
assuming your source file encoding matches the encoding of your locale.
Oddly enough what worked was going at it normally and putting the special character into a string it's so ridiculously simple I didn't even think to use it.
#include<iostream>
using namespace std;
int main()
{
string piece="♗";
cout<<piece;
}

Using chdir() Causes Segmentation Fault

I'm writing a batch emulator as a personal project. I'm trying to implement the cd command using chdir() from unistd.h. However, using this causes a segfault.
main.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
//Custom headers
#include "splitting_algorithm.hpp"
#include "lowercase.hpp"
#include "chdir.hpp"
//Used to get and print the current working directory
#define GetCurrentDir getcwd
using namespace std;
int main(int argc, char* argv[])
{
string command;
//Begin REPL code
while (true)
{
//Prints current working directory
cout<<cCurrentPath<<": ";
std::getline(std::cin, command);
vector<string> tempCommand = strSplitter(command, " ");
string lowerCommand = makeLowercase(string(strSplitter(command, " ")[0]));
//Help text
if(tempCommand.size()==2 && string(tempCommand[1])=="/?")
{
cout<<helpText(lowerCommand);
}
//Exit command
else if(lowerCommand=="exit")
{
return 0;
}
else if(lowerCommand=="chdir")
{
cout<<string(tempCommand[1])<<endl;
chdir(tempCommand[1]);
}
else
cout<<"Can't recognize \'"<<string(tempCommand[0])<<"\' as an internal or external command, or batch script."<<endl;
}
return 0;
}
chdir.cpp:
#include <cstdlib>
#include <string>
#include <unistd.h>
void chdir(std::string path)
{
//Changes the current working directory to path
chdir(path);
}
Strangely enough, using cout to get the path for chdir works perfectly fine. How do I fix this?
You have recursive, unterminated behaviour in Your code. This overflows the stack.
Try to insert breakpoint in void chdir(std::string path) and see what happens.
You will see that the function chdir calls itself, and in turn calls itself again, and again and... well, segmentation fault.
Also, try to see what "call stack" is in the debugger, this issue is very visible there.
You should invoke the underlying chdir function using
::chdir(path.c_str());
or you will just call your own method again.
In unistd.h, chdir is defined as:
int chdir(const char *);
So you must call it with a const char* argument or the compiler will search for another function called "chdir" which take a std::string argument and use that instead.

When do I use '#include <string>' at the start of a C++ program?

I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>

FindFirstFile() show address

I used function FindFirstFile() but i received only memory address - not a file name.
#include <stdafx.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
WIN32_FIND_DATA File_Data;
FindFirstFile(TEXT("C:\\Users\\user\\Desktop\\temp\\*.tmp"), &File_Data);
cout<<File_Data.cFileName;
cin.get();
return 0;
}
Can anybody help me?
You are probably compiling using the Unicode character set, which means that windows API's will default all character strings to the wide version (wchar_t vs char). Try using the wide output version of cout (wcout):
wcout<<File_Data.cFileName;