0

我有一个包含键和值的文本文件,如下所示:

keyOne=1
keyTwo=734
keyThree=22.3
keyFour=5

键只是小写和大写字母,就像我的例子一样。这些值是整数或浮点数。每个键和值由等号 (=) 分隔。现在我想将这些值读入我程序中的变量中。

这是我尝试读取值的代码:(我省略了将值存储在程序变量中的部分,现在将它们打印出来以进行演示。)

std::fstream file(optionsFile, std::fstream::in);

if (file.good()) {
  int begin;
  int end;
  std::string line;

  while(std::getline(file, line)) {

    // find the position of the value in the line
    for (unsigned int i = 0; i < line.length(); i++) {
      if (line.at(i) == '=') {
        begin = i + 1;
        end = line.length();
        break;
      }
    }

    // build the string... it starts at <begin> and ends at <end>
    const char *string = "";
    for (int i = begin; i < end; i++) {
      string += line.at(i);
    }

    // only gibberish is printed in the following line :(
    std::cout << "string=" << string << std::endl;
  }
}

我不明白为什么它不会打印值..而是只打印奇怪的东西甚至什么都没有

请帮助这让我的精神如此沉重:(

4

2 回答 2

1

如前所述,c/c++ 中的原生字符串类型不支持直接连接,因为它们本质上是指向某些预分配内存的指针。当字符串应该是可变的时,您应该始终使用 std::string 。

顺便说一句,考虑以下重构:

void process_option (const std::string& a_key, const std::string& a_value)
{
    std::cout << a_key << " <-- " << a_value << std::endl;
}

void read_options (std::istream& a_in, const char* a_source)
{
    int line_n = 0;
    std::string line;
    while (std::getline(a_in, line))
    {
        ++ line_n;
        std::string::size_type p = line. find('=');
        if (p == line. npos)
        {
        //  invalid_entry(a_source, line_n);
            continue;
        }

        process_option(
            line. substr(0, p), // key
            line. substr(p + 1, line. find_first_of("\t\r\n", p + 1)) // value
        );
    }
}

void read_options (const char* a_filename)
{
    std::ifstream file(a_filename);
    if (! file)
    {
    //  read_error(a_filename);
        return;
    }
    read_options(file, a_filename);
    file. close();
}

void read_options (const std::string& a_filename)
{
    read_options(a_filename. c_str());
}
于 2013-08-24T16:31:45.233 回答
1

您正在使用没有正确分配内存的 C 样式字符串(char 数组),并且您只是在使用指针进行操作,因此您没有将字符附加到您的字符串中:

   // build the string... it starts at <begin> and ends at <end>
const char *string = "";
for (int i = begin; i < end; i++) {
  string += line.at(i);
}

改用std::string

/// build the string... it starts at <begin> and ends at <end>
std::string str;
for (int i = begin; i < end; i++) {
  str += line.at(i);
}

或者手动分配内存,使用正确的索引,用 '\0' 字符终止字符串,不要忘记在不再需要它后删除字符串:

char *string = new char[end - begin + 1];
int j = 0;
for (int i = begin;  i < end; i++) {
  string[j++] = line.at(i);
}

// Don't forget to end the string!
string[j] = '\0';

// Don't forget to delete string afterwards!
delete [] string;

所以,只需使用std::string.

编辑你为什么要混合 C 字符串std::string

于 2013-08-24T15:46:26.887 回答