Zepx Posted December 21, 2005 Share Posted December 21, 2005 (edited) Hello,I have this problem. I tried to rename a file, it would success if I do it in the way wherechar mychar[]= "file.txt";However, I tried using from string, and converting that string to char, the compiler says that it is impossible to convert that string to a char. So I was wondering if is there any other solutions or can I actually really convert string to char?Anyhow, here is my idea:I am actually building this program to allow users to rename the file to something, which means they have to tell me (the program) the path to that folder. And I will rename that folder to a new name.Here is my code:#include <iostream>#include <stdlib.h>#include <string>#include <stdio.h>using namespace std;int main(int argc, char *argv[]){ string test; cout << "Please insert Folder path" << endl; cout << "Path: "; getline (cin, test); cout << test << endl; char mychar[] = test; char mychar2[] = test + "2"; // which will rename the folder to folder2 int result = rename(mychar, mychar2); if(result != 0) cout << "An Error Have Occured!" << endl; else cout << "Rename Successful!" << endl; system("PAUSE"); return 0;} Edited December 21, 2005 by Zepx Link to comment Share on other sites More sharing options...
pZy Posted December 23, 2005 Share Posted December 23, 2005 To convert a std::string to an const char* (which most C-functions need) you have to use the c_str() member function. string s0 = "testfile";string s1 = "testfile2";rename(s0.c_str(), s1.c_str());keep it up. Link to comment Share on other sites More sharing options...
Zepx Posted December 26, 2005 Author Share Posted December 26, 2005 Weeee! Thank you so much! Link to comment Share on other sites More sharing options...
I_Broke_My_MHZ Posted January 5, 2006 Share Posted January 5, 2006 Correct me if I'm wrong butcout << "Please insert Folder path" << endl;can just as well becout << "Please insert Folder path\n"; Link to comment Share on other sites More sharing options...
cyprod Posted January 5, 2006 Share Posted January 5, 2006 (edited) well, that depends on what you want to do exactly. when you docout << "Please insert Folder path\n";you are adding that string to the screen buffer and not actually writing it to the screen. It won't be written to the screen until either the buffer is filled or flushed. on the other handcout << "Please insert Folder path" << endl;the endl has an automatic flush buffer built into it so as soon as it's sent into the buffer, the buffer is flushed and the text appears on the screen. so basically, "\n" will write it to the buffer with a new line but not put it onto the screen until either the program exits or the buffer is flushed while endl adds the new line as well as flush the buffer and immediatly place the text on the screen. Edited January 5, 2006 by cyprod Link to comment Share on other sites More sharing options...
I_Broke_My_MHZ Posted January 6, 2006 Share Posted January 6, 2006 That was explained perfectly. Is there anything that will flush the buffer without creating a newline like endl does? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now