0
#include<iostream>
#include<windows.h>
#include<string.h>
using namespace std;
main()
{
    string kelime="dir";
    system(kelime);
}

if i do like this: char kelime[10]="dir";

It is working but i want to do it with string? What is the problem, how can I do it?

3
  • Try system(kelime.c_str()); Commented Dec 25, 2016 at 14:58
  • your compiler must be ancient for string.h to have std::string. Commented Dec 25, 2016 at 15:48
  • please do mention the exact error you are getting Commented Dec 25, 2016 at 16:13

2 Answers 2

4
system(kelime.c_str());

system() (and many other APIs) are designed to be called from C, and take so-called C-strings, which are a NUL-terminated array of single-byte ASCII characters.

Use std::string::c_str() to get a C string from your C++ std::string.

Sign up to request clarification or add additional context in comments.

1 Comment

I did not kno that. Thank you for explaining.
0

Use kelime.c_str() to access the old char style

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.