成员数组在每次循环迭代中丢失内容
Member array losing contents on each loop iteration
我有一个方法可以解析令牌的文件,并将它们存储在 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());
相关文章:
- 擦除while循环中迭代的元素
- 对于set上的循环-获取next元素迭代器
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 将多个 for 循环组合成单个迭代器
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 使用一个 for 循环如何迭代和测试迭代器值?
- 循环挂起迭代的 std::擦除 on std::list
- 使用基于范围的循环迭代矢量时出现问题
- 整个循环花费的时间比其迭代总和要长
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- C++ Code 在 for 循环期间不会累积变量中的总和,仅提供最终迭代值
- Visual Studio 2013 C 的迭代循环
- 矢量迭代循环引发错误
- C++:如何将数据从迭代循环输出到列中
- 迭代循环重复的次数比我预期的要多
- c++ 11线程在迭代循环时间
- 迭代循环与索引循环