I have one path e.g. "C:\home\my folder". I want to convert it into "C:\\home\\my folder". Please suggest me how can i use any function call to do this?
Thanks in advance.
The best way to do this could be by using the Boost string algorithm library.
http://www.boost.org/doc/libs/1_46_1/doc/html/string_algo.html
Using the following command :
std::string newPath = boost::replace_all_copy(testStr, "\", "\\");
This would result in the replaced and newly formed string.
Hope this helps.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream lire("data.txt",ios::in);
string text ;
getline(lire,text); //text contains your repertory
cout<<text<<endl;
for (int i = 0 ; i < text.size();i++)
{
if(text[i] == '\\')
{
text.insert(i,"\\") ;
i++;
}
}
cout<<text<<endl;
return 0;
}
The following program works for me.
#include <iostream>
#include <string>
#include <Shlobj.h>
int main()
{
std::string myPath;
std::cout << "Enter path to create: ";
std::cin >> myPath;
std::cout << "Path from user: " << myPath << "\n";
::SHCreateDirectoryEx(nullptr, myPath.c_str(), nullptr);
return 0;
}
Sample Run
As you can see the path taken from user input only uses a single back slash but the directory is successfully created.
Enter path to create: C:\a_path
Path from user: C:\a_path
I think you chasing the wrong problem, assuming the reason you asked this question is because your call to the SHCreateDirectoryEx function failed.
Notes:
When some Win32 API functions fail (e.g., CraeateDirectory) you must call GetLastError to actually get the error code, but this isn't the case for SHCreateDirectoryEx (i.e., it returns the failure code directly).
The SHCreateDirectoryEx function also works for me if I use forward slashes.
Related
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
constexpr auto MAX_NUM_LEN = 10;
int main() {
string a_num[MAX_NUM_LEN];
cout << "Type your full ID" << endl;
cin.getline(a_num, MAX_NUM_LEN, '.');
cout << "\nyour input is: " << a_num;
}
Link to my embedded image of the program i did in VS code
[1]: https://i.stack.imgur.com/TM7BC.jpg
Use char a_num[MAX_NUM_LEN]; instead of string a_num[MAX_NUM_LEN];
Your code will run.
when you use cin.getline() function, then its first parameters would be either a constant character pointer or a character array name.
Please check following resources to learn more about getline
std::getline (string)
getline (string) in C++
C++ program to read string using cin.getline()
std::cin.getline( ) vs. std::cin
I've seen a couple of solutions to this question, but I'm still running into an issue. I've tried several of the solutions I've seen on this site, but I keep having the same issue. I'm not sure how to make this work, and I'm also not sure why it's not working, since I'm really new to C++.
A brief explanation of what I am attempting to do: I'm writing a simple old-school text-based story/game for a video game design club at my school, and at several points I have the user make decisions, or input things such as their name, which is then used in an output line, as it would be referenced several times. I have had no problem doing that, using something simple like this:
#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;
int main(){
string name, place ;
cout << "???: What is your name? ";
getline (cin, name);
cout << "???: Hello, " << name << "!\n" << "\n";
}
My problem is that I'd like to have the text appear one character at a time, like dialogue, but whenever I try to use something I've seen written by others, it doesn't seem to like it very much. The only one that I tried that I can now find again is this:
#include <iostream>
#include <unistd.h> // include <windows.h> on windows
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
usleep(60000); // use Sleep on windows
}
}
int main()
{
type_text("Hej hej hallÄ!");
}
Apparently there is some sort of conflict regarding my attempt to output the name back to the user when I try to use that code with what I've written. I'm not really sure what the problem is, since I'm so new to C++, can anyone help me out?
Consider using std::this_thread::sleep_for, as it is standard C++11. Example:
#include <iostream>
#include <thread> // standard C++11
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
}
int main()
{
type_text("Hello, World!");
}
If you have access to a C++14 compiler, you can simply make use of the std::chrono user-defined literals and have a more "natural" syntax:
using namespace std::literals;
std::this_thread::sleep_for(60ms);
Can someone explain to me how to properly search for a "tab" character stored in a string class?
For example:
text.txt contents:
std::cout << "Hello"; // contains one tab space
User enters on prompt: ./a.out < text.txt
main.cpp:
string arrey;
getline(cin, arrey);
int i = 0;
while( i != 10){
if(arrey[i] == "\t") // error here
{
std::cout << "I found a tab!!!!"
}
i++;
}
Since there is only one tab space in the textfile, I am assuming it is stored in index [0], but the problem is that I can't seem to make a comparison and I don't know any other way of searching it. Can someone help explain an alternative?
Error: ISO C++ forbids comparison between pointer and integer
First of all, what is i? And secondly, when you use array-indexing of a std::string object, you get a character (i.e. a char) and not a string.
The char is converted to an int and then the compiler tries to compare that int with the pointer to the string literal, and you can't compare plain integers with pointers.
You can however compare a character with another character, like in
arrey[i] == '\t'
std::string::find() might help.
Try this:
...
if(arrey.find('\t') != string::npos)
{
std::cout << "I found a tab!!!!";
}
More info on std::string::find is available here.
Why not using what C++ library provides? You could do it like this:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string arrey;
getline(cin, arrey);
if (arrey.find("\t") != std::string::npos) {
std::cout << "found a tab!" << '\n';
}
return 0;
}
The code is based on this answer. Here is the ref for std::find.
About your edit, how are sure that the input is going to be 10 positions? That might be too little or too big! If it is less than the actual size of the input, you won't look all the characters of the string and if it is too big, you are going to overflow!
You could use .size(), which says the size of the string and use a for loop like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string arrey;
getline(cin, arrey);
for(unsigned int i = 0; i < arrey.size(); ++i) {
if (arrey[i] == '\t') {
std::cout << "I found a tab!!!!";
}
}
return 0;
}
I have this code in C++:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string str;
ifstream file("file.txt");
file >> str;
cout << str;
return 0;
}
I have file.txt in the same directory as main.cpp. I get no output from this, I've tried specifying full filepath to the file and still no result and tried it on few different machines too.
Does anybody know what I'm doing wrong?
What you're interested in is the current working directory for your program, i.e. where your text file is supposed to be if you don't qualify it with a full or relative path.
You can get it at runtime with getcwd (linux) or _getcwd (windows).
Edit: I agree with Andy, you should anyway check for errors when opening files. You could have caught this earlier (i.e. file not found), e.g.
(pseudocode ahead for illustrative purposes)
#include <unistd.h>
// Warning: linux-only, use #ifdefs and _getcwd for windows OS
std::string get_working_path() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
return std::string(cwd);
else
return std::string("");
}
int main() {
std::string str;
std::ifstream file("file.txt");
if (file >> str)
std::cout << str;
else {
std::cout << "File not found in cwd: " << get_working_path();
// abort
}
// ...
}
Please forgive me if the answer to this is simple, but I have NO idea what is causing this. The PathCombineA function is somehow modifying the mypath variable. If you run the program you will see what I mean. (You must link Shlwapi.lib)
#include <Windows.h>
#include <Shlwapi.h>
#include <iostream>
using namespace std;
int main()
{
CHAR temp[MAX_PATH];
CHAR mypath[MAX_PATH];
GetModuleFileNameA(NULL, mypath, MAX_PATH);
GetTempPathA(MAX_PATH, temp);
LPSTR name = PathFindFileNameA(mypath);
cout << mypath << endl;
PathCombineA(name, temp, name);
cout << mypath << endl;
getchar();
return 0;
}
Output before PathCombineA
C:\Users\Owner\Desktop\etc\Debug\etc.exe
Output after PathCombineA
C:\Users\Owner\Desktop\etc\Debug\C:\Users\Owner\AppData\Local\Temp\etc.exe
If you guys have any idea what is going on, please tell me!
Thanks!
PathFindFileNameA is returning a pointer to the last part of the string in mypath.
You then pass that pointer into the mystring buffer as the output parameter to PathCombineA.
If you don't want mystring to be modified, you'll need yet another buffer to hold the output of PathCombineA.