使用指针c ++的问题

时间:2013-11-29 21:09:12

标签: c++ arrays string pointers char

#include <iostream>
#include <fstream>
#include <cstring>
#include <map>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    //define a bool to determine if you're still in the file's header section or not
    bool header = true;
    //define the map as a multidimensional string array that stores up to 100 z-levels
    //and 50x50 tiles per z level
    char* worldmap[100][50][50];
    int zLevel=0;
    //header declaration map
    map<string, char*> declarations;
    //create an input file stream object
    ifstream file;
    //open file
    file.open("map1.dmm");
    //validate file
    if(!file.good()){return 1;}

    //begin reading the file
    while(!file.eof()){
        //create a character array to write to
        char line[512];
        //read the file line by line; write each line to the character array defined above
        file.getline(line, 512);

        //header check
        if(header){
            if(!line[0]){
                header = false;
                break;
            }else{
                bool declaringKey=true;
                char* key={};
                char* element={};
                char* token[20]={};
                token[0] = strtok(line, "\"()");
                for(unsigned int n = 0;n<20;n++){

                    if(n>0)token[n] = strtok(NULL, "\"()");
                    //cout << token[0] << endl;
                    if(!token[n] || (token[n])[1] == '=')continue;
                    if(declaringKey){

                        key = token[n];
                        declaringKey=false;

                    }else{
                        //cout << "pow" <<endl;
                        element = token[n];
                        cout<<"declarations[" << key << "] = " << element << endl;
                        declarations.emplace(key,element); //<-------------- problem line, i think
                        cout << declarations[key] << endl;

                    }

                }declaringKey=true;

            }
        }else{
            if(!line[0]) {
                zLevel++;
                continue;
            }

        }

    }
//string test = "aa";

    return 0;

}

我正在尝试创建一个从文本文件加载简单地图的地图加载器。我知道还有其他的地图加载器可用,但是大部分都比我需要的要多得多。到目前为止,此代码仅读取标题,该标题基本上定义了每组字符表示为平铺的内容,例如:“aa”=“sand tile”

问题是,当我将键/元素放入声明映射时,它似乎对所有键使用相同的元素。我假设这是因为通过定义一个字符指针它始终指向相同的数据,并且只用于更改该指针所包含的值,而不是分配新数据并将它们分开。

但这提出了另一个问题,为什么它会使用相同的元素赋予不同的密钥,即使它们都是指针......?反正,

如何使所有键/元素都是独立的字符数组,而不是指向由数组划分的完全相同的空格的指针..?

编辑:您可以假设代码的工作方式不同于它将相同的元素存储到所有键的事实。此外,它存储的元素始终是从文件的标题部分解析出来的最后一个元素。

1 个答案:

答案 0 :(得分:2)

也可以使用std::string作为值。这应该可以解决您当前的问题。

那就是说,使用stream.eof()来控制循环读取值!这是行不通的。相反,总是尝试从流中读取,然后验证读取是否成功,例如:

while (file.getline(line, sizeof(line))) {
    // ...
}

就个人而言,我不会读入固定大小的缓冲区而是使用std::string代替:

for (std::string line; std::getline(file, line); ) {
    // ...
}

从这一点开始,我也不会使用strtok(),而是使用std::string的成员或合适的算法。这样我也不会误入歧途,考虑存储指针是一个好主意(更不用说我不能处理指针,因此,我的程序不使用它们)。