简体   繁体   English

C++:迭代字符串向量并使用 putenv 的索引

[英]C++: Iterating over a string vector and using the index for putenv

I have a vector of strings which was created from parsing a config file.我有一个字符串向量,它是通过解析配置文件创建的。 All the strings should be in the format key=value .所有字符串都应采用key=value格式。 I'd like to iterate over the vector, and use the putenv function to set an environment variable to the key-value pair.我想遍历向量,并使用 putenv 函数将环境变量设置为键值对。

The code:编码:

for(auto it = settings.begin(); it != settings.end(); it++) {
      try {
         auto i = it - settings.begin();
         cout << i << endl;
         putenv(settings.at(i));
      } catch (...) {
         cout << "Config is not in the format key=value ... please correct" << endl;
      }
   }

This throws the error:这会引发错误:

cannot convert ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘int putenv(char*)’

I'm very new to C++, and all these variable types and pointers are confusing me.我对 C++ 很陌生,所有这些变量类型和指针都让我感到困惑。

You're mixing C and C++ stuff.你正在混合 C 和 C++ 的东西。

  • Your vector contains C++ strings, std::string .您的向量包含 C++ 字符串std::string
  • putenv is an "old" function expecting a pointer to a char buffer, ie a C-string. putenv是一个“旧”函数,它需要一个指向char缓冲区的指针,即一个 C 字符串。

Fortunately, std::string makes it easy to get one of those :幸运的是, std::string可以轻松获得其中之一

putenv(settings.at(i).c_str());
//                   ^^^^^^^^

However, there is still a problem there.但是,那里仍然存在问题。 putenv takes "ownership" of the buffer you give it, and expects it to last "forever". putenv获取您提供的缓冲区的“所有权”,并希望它“永远”持续。 Yours won't;你的不会; it'll only be there until the std::string is modified or destroyed.它只会在std::string被修改或销毁之前存在。 Not good enough!还不够好!

Conventionally, we use C's strdup here to allocate a copy of the char buffer.按照惯例,我们在这里使用 C 的strdup来分配char缓冲区的副本。 It's then putenv 's (or the OS's) responsibility to free it later.然后putenv (或操作系统)有责任稍后释放它。

putenv(strdup(settings.at(i).c_str()));
//     ^^^^^^^                      ^

Since putenv is not in the C or C++ standard, but is provided by POSIX, you should check the manpages for your operating system to make sure you're using it correctly.由于putenv不在 C 或 C++ 标准中,而是由 POSIX 提供,因此您应该检查操作系统的联机帮助页以确保正确使用它。

The error is caused by your call to putenv(), which expects a pointer to char 's.该错误是由您调用 putenv() 引起的,它需要一个指向char的指针。 Your vector contains C++ strings (std::string)...您的向量包含 C++ 字符串 (std::string)...

You can try this:你可以试试这个:

for (auto setting : settings) {

     // ... putenv() is inconsistent across compilers, so use setenv() instead...         
     std::string key = item.substr( 0, item.find_first_of( "=" ) ) ;
     std::string val = item.substr( key.length()+1 ) ;

     if ( setenv( key.c_str(), val.c_str(), 1 ) != 0 ) {
        cerr << "setenv(" << key << ',' << val << ") has failed: " << strerror( errno ) << endl;
     }
}

From what I've read, putenv should be avoided in new code, and setenv should be used, as it makes a copy of its arguments, regardless of compiler version.从我读过, putenv应避免在新的代码,并setenv应该使用,因为它使得它的参数的副本,无论编译器版本。

( setenv is in stdlib.h) setenv在 stdlib.h 中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM