成员数组在每次循环迭代时丢失内容

时间:2014-02-24 05:38:50

标签: c++ arrays

我有一个方法解析一个文件的标记,并将它们存储在一个char *数组中...这看起来有点工作,但这些值没有被保存到对象中。我可以遍历,每次我在do / while重新启动时,我的调试器显示我的成员数组已重置回"" - 陌生人仍然是每次我为数组赋值,它用当前数据重新填充先前的条目。

示例输出:

Parsing
1 2 8
Token 0: 1
Token 1: 2
Token 2: 8
2 3 3
Token 3: 2
Token 4: 3
Token 5: 3

此时调试器中的tokens成员数据:

tokens  char *[25]  0x603020    
    tokens[0]   char *  0x7fffffffdc70 "2"  
    tokens[1]   char *  0x7fffffffdc72 "3"  
    tokens[2]   char *  0x7fffffffdc74 "3"  
    tokens[3]   char *  0x7fffffffdc70 "2"  
    tokens[4]   char *  0x7fffffffdc72 "3"  
    tokens[5]   char *  0x7fffffffdc74 "3"
    ...

因此看起来数据是正确的,但仅在循环期间才开启。我在这里搞砸了什么?

的main.cpp

#include "includes.h"

#include <iostream>
using namespace std;

int
RoutingManager::ParseInputFile(char* filePath, const int MAX_CHARS_PER_LINE,
                                const int MAX_TOKENS_PER_LINE, const char* const DELIMITER)
{

    cout << "Parsing\n";

    ifstream theFile;
    theFile.open(filePath);

    if(!theFile.good())
    {
        cout << "No good\n";
        return 1;
    }

    int tokenIndex = 0;

    do
        {
            char buf[MAX_CHARS_PER_LINE];
            theFile.getline(buf, MAX_CHARS_PER_LINE);

            cout << buf << endl;

            this->tokens[tokenIndex] = strtok(buf, DELIMITER);
            if (this->tokens[tokenIndex])
            {
                tokenIndex++;

                for (int i = 0; i < MAX_TOKENS_PER_LINE; i++, tokenIndex++)
                {
                    this->tokens[tokenIndex] = strtok(NULL, DELIMITER);
                    cout << "Token " << tokenIndex-1 << ": " << this->tokens[tokenIndex-1] << endl;
                    if (!tokens[i])
                        break;
                }

                tokenIndex--;
            }
        }while (!theFile.eof());

        return 0;

}


int main(int argc, const char* argv[])
{
    RoutingManager *manager = new RoutingManager();
    manager->ParseInputFile("/home/caleb/Documents/dev/cs438/tote2/mp2/build/topo.txt", 10, 3, " ");

    return 0;
}

·H

#pragma once

#include <fstream>
#include <cstring>

class RoutingManager
{
public:
    int ParseInputFile(char* filePath, const int MAX_CHARS_PER_LINE = 512,
                                const int MAX_TOKENS_PER_LINE = 20, const char* const DELIMITER = " ");

public:
    char* topoFilePath;
    char* msgFilePath;
    static const int MAX_TOKENS = 25;
    char* tokens[MAX_TOKENS];
};

编辑:我想知道这是否与strtok()修改我在数组中保存的数据有什么关系?

2 个答案:

答案 0 :(得分:3)

strtok为您提供指向buf的指针 - 它不会分配任何内存。然后你用新数据重新填充buf,并且所有这些指针只是指向新数据中的一些随机点,即之前恰好出现旧标记的点,但不是更多。

帮自己一个忙,并将tokens更改为vector<string>

答案 1 :(得分:0)

当你说'......没有将值保存到对象'时,不确定要保存的对象。但无论如何,你的代码可以有所简化:

int tokenIndex = 0;
char* pch;

do
    {
        char buf[MAX_CHARS_PER_LINE];
        theFile.getline(buf, MAX_CHARS_PER_LINE);

        cout << buf << endl;

        pch = strtok(buf, DELIMITER);

        for (int i = 0; i < MAX_TOKENS_PER_LINE && pch; i++)
        {
            this->tokens[tokenIndex] = pch;
            cout << "Token " << tokenIndex << ": " << this->tokens[tokenIndex] << endl;
            tokenIndex++;
            pch = strtok(NULL, DELIMITER);

        }

        tokenIndex = 0;

    } while (!theFile.eof());