I want to issue the echo command inside a c++ file.
All i need to do is
echo "xml::/var/some.xml" >> /var/config
In C++ file, I tried,
system("echo" + "xml::/var/some.xml" +">> /var/config");
But it throws invalid operands of types const char[6] and const char[46] to binary operator +.
Need help
I guess this is one where you may be able to get away with "can has codez?":
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char **argv) {
std::copy(argv+1, argv+argc, std::ostream_iterator<char *>(std::cout, ""));
return 0;
}
You could just output the data yourself using stdio methods fopen/fputs/etc..
http://linux.die.net/man/3/fputs
Try
#include <string>
system(std::string("echo" + "xml::/var/some.xml" +">> /var/config").cstr())
you are passing raw character strings which do not support the + operator -- so try to use std::string instead.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
I am trying to substring some expressions into individual tokens such as !, &, | (), etc. What I am having trouble with is the fact that when I try to make a sub-string of "!(S&B|H)&!(S&J|R)&!(P)" with the cout line below, I get: "(S&J|R)&!(P)", when I thought it should be: "(S&J|R)". It either is beyond what I have seen or just so simple that I just am not getting it. Any help would help a lot. Thanks.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
string name = "!(S&B|H)&!(S&J|R)&!(P)";
cout<<name.substr(10,16)<<endl;
return 0;
}//Main
I did not understand your question well but if you want to get
(S&J|R)
You should do:
name.substr(10,7)
The second parameter is the length.
This has worked fine on some compilers... Is there a way of doing this were it will just work without it being a problem with different compilers on c++11 or c++14?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void save_file() {
string file;
ofstream os;
cout << "Save As: ";
getline(cin, file, '\n');
os.open(file + ".dat");
//rest of code
}
error: no viable conversion from 'basic_string, std::allocator >' to 'const char *'
So I google it, found some answers, or in this case, canswers (cancers), tried
os.open(file.c_str() + ".dat");
error: invalid operands to binary expression ('const char *' and 'const char *')
Accoding to the C++11 standard 27.9.1.10 one of the constructors for a basic_ofstream is:
explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
This means that any standard compliant compiler should be able to compile:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string file = "temp";
ofstream os;
os.open(file + ".dat");
}
Live Example
Don't forget that you need to use the -std=c++11 or higher flag when compiling.
"+" operator cannot be used for the C-style strings. Try this:
string name = file+".dat";
os.open(name.c_str());
You create the std::string type as a concatenation in c++ style and then pass it to open as a c string.
In C++11, os.open( file + ".dat" ) works just fine. Pre C++11, there was no std::ofstream::open which took a string, so you had to write os.open( (file + ".dat").c_str() ). Note the parentheses and where the .c_str() goes---you have to concatenate with the std::string first, and only call .c_str() on the results.
When using ifstream class to read words from an input file, I have used the following expression:
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ifstream inputStream(myFile.txt);
std::string myString;
myFile.open()
while(myFile.good())
{
myFile >> myString;
printf("%s \n", myString);
}
return 0;
}
The contents of myFile.txt are:
" This is a simple program. "
The compiles and executes as expected using g++ compiler.
However, the same code when compiled using msvc 2008, returns error at the extraction operator (>>) requiring me to replace the std::string with either an initialized character array or any of the supported native types.
This threw me off as I was expecting the usage of the standard library to be same across implementations.
I understand the compile error and know the way to fix it via using c_str().
But, it would help me a great deal, if someone could clarify why the usage for the standard library is different across platforms.
To me it is not starndard anymore !!
EDIT: Code updated to be complete. Content of myFile.txt updated.
Chances are that you forgot to #include <string>. Without it, Microsoft's version of <iostream> (and such) include enough of a declaration of std::string for some things to work, but other parts are missing, so you get strange, seemingly inexplicable failures.
One of the things that's missing is most of the operator overloads for std::string, which is exactly what you seem to be missing.
As an aside, while (myfile.good()) ... is pretty much a guaranteed bug -- you probably want:
while (myfile>>myString)
std::cout << myString << " \n";
Alternatively, you could do the job with a standard algorithm:
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
int main() {
std::ifstream myfile("input.txt");
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, " \n"));
return 0;
}
The following compiles fine for me on MSVC 2010:
std::ifstream inputStream;
std::string myString;
inputStream.open("myFile.txt", std::ifstream::in);
while(inputStream.good())
{
inputStream >> myString;
}
Note: without using std::ifstream::in as my open mode, I got the same error as you. I suggest you check what value you have for this parameter.
I want to compare two user input strings, but not able to do so...
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv0[])
{
string my_string;
string my_string2;
cout<<"Enter string"<<endl;
cin>>my_string;
cout<<"Enter 2nd string"<<endl;
cin>>my_string2;
cout<<my_string<<" "<<my_string2;
strcmp(my_string,my_string2);
int result;
result= strcmp(my_string,my_string2);
cout<<result<<endl;
return 0;
}
This error is appearing.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' c:\users\asad\documents\visual studio 2008\projects\string\string\string.cpp 23 String
Since you're using std::string, strcmp is unnecessary -- you can just use <, ==, !=, etc.
Your includes:
Since you are including standard headers, they should be in <>
#include <string>
#include <iostream>
#include with "" is generally used for your own header files, not standard header files.
You are using C++, and therefore need not use strcmp. In C++, you can simply use == & != to compare two strings.
if (my_string == my_string2) result = 0;
else result = 1;
Also, if you want to convert a string to a const char*, you can use mystring.c_str()
If you want to use strcmp note that it takes different parameters than the ones you used.
http://www.cppreference.com/wiki/c/string/strcmp
Another way to do this is also
result= strcmp(my_string.c_str(),my_string2.c_str());