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

Member array losing contents on each loop iteration

本文关键字:迭代 循环 数组 成员      更新时间:2023-10-16

我有一个方法可以解析令牌的文件,并将它们存储在 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"
    ...

因此,数据似乎是正确的,但仅在循环期间打开。我在这里搞砸了什么?

主.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 << "Parsingn";
    ifstream theFile;
    theFile.open(filePath);
    if(!theFile.good())
    {
        cout << "No goodn";
        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() 修改我保存在数组中的数据有关?

strtok为您提供指向buf的指针 - 它不会分配任何内存。然后你用新数据重新填充buf,所有这些指针都指向新数据中的一些随机点,旧令牌恰好在以前的地方,但不再是。

帮自己一个忙,把tokens改成vector<string>

当您说"...这些值不会保存到对象中。 但无论如何,您的代码可以简化一些:

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());