#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{;
int arraylen = 7, arraylen_new = 0, i = 0, nextstep = 0;
string line, field, junkText;
string* randNum = new string[arraylen];
string* phrase = new string[arraylen];
ifstream randomTxt;
arraylen = 5;
randomTxt.open("random.txt");
while(!randomTxt.eof())
{
getline(randomTxt,line);
stringstream ss(line);
nextstep = 0;
while(getline(ss,field,','))
{
cout << i << endl;
/*if(nextstep == 0)
{
randNum[i] = field;
cout<< "number " << i << ": " << randNum[i] << endl;
nextstep = 1;
}
else if(nextstep == 1)
{
junkText = field;
cout<< "junk " << i << ": " << junkText << endl;
nextstep = 2;
}
else
{
items[i] = field;
cout<< "items " << i << ": " << items[i] << endl;
}*/
if(i == (arraylen-1))
{
arraylen_new = arraylen + 1;
string* temp1 = new string[arraylen_new];
string* temp2 = new string[arraylen_new];
copy(randNum, (randNum+arraylen), temp1);
delete [] randNum;
randNum = temp1;
copy(phrase, (phrase + arraylen), temp2);
delete [] phrase;
phrase = temp2;
arraylen = arraylen_new;
}
}
cout<<"test"<<endl;
i++;
}
randomTxt.close();
/*for(int i = 0; i < arraylen;i++)
{
cout<< "UPC " << i << ": " << UPC[i] << "t" << "Phrase " << i << ": " << phrase[i] << endl;
}*/
return 0;
}
检查i
的值时,打印输出如下:
0
0
0
test
test
2
2
2
test
test
4
4
4
依此类推,直到我的文件被完全读取。我的迭代需要正确匹配,因为我使用它来将行的每一部分保存到单独的数组中。知道为什么它似乎跳过内部 while 循环吗?
以下是文本文件的片段:
333331234567,blah,"this is nonsense"
222221234567,word,"yadda yadda words"
111111234567,thing,"total gibberish stuff"
我不重现您的问题。
但是我不得不修复您的示例代码中的一些名称,所以也许我不小心修复了它?
这是我的版本,带有一些额外的 DEBUG 打印语句。
另请注意,检查eof()
很少有用,因此将外部循环更改为 while (getline(randomTxt, line))
。
然后对于i
循环索引,我认为编写为 for 循环会更自然。
住在科里鲁
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
/// returns: new array length
int main() {
size_t arraylen = 7;
std::string line;
std::string *randNum = new std::string[arraylen];
std::string *phrase = new std::string[arraylen];
std::ifstream randomTxt;
randomTxt.open("random.txt");
for (size_t i=0; getline(randomTxt, line); ++i)
{
std::cout << "DEBUG LINE: '" << line << "'n";
//nextstep = 0;
std::stringstream ss(line);
std::string field;
while (getline(ss, field, ',')) {
std::cout << "DEBUG INNER " << i << " '" << field << "'n";
if (i == (arraylen - 1)) {
size_t arraylen_new = arraylen + 1;
{
std::string *tmp = new std::string[arraylen_new];
copy(randNum, randNum + arraylen, tmp);
delete[] randNum;
randNum = tmp;
}
{
std::string *tmp = new std::string[arraylen_new];
copy(phrase, (phrase + arraylen), tmp);
delete[] phrase;
phrase = tmp;
}
arraylen = arraylen_new;
}
}
}
}
指纹
DEBUG LINE: '333331234567,blah,"this is nonsense"'
DEBUG INNER 0 '333331234567'
DEBUG INNER 0 'blah'
DEBUG INNER 0 '"this is nonsense"'
DEBUG LINE: '222221234567,word,"yadda yadda words"'
DEBUG INNER 1 '222221234567'
DEBUG INNER 1 'word'
DEBUG INNER 1 '"yadda yadda words"'
DEBUG LINE: '111111234567,thing,"total gibberish stuff"'
DEBUG INNER 2 '111111234567'
DEBUG INNER 2 'thing'
DEBUG INNER 2 '"total gibberish stuff"'
笔记
- 您可能想要创建一个
grow_array
函数,因为您复制了该代码 - 您可能希望按因子增长,而不是一次增长 1 个元素;这根本不会扩展并导致糟糕的性能/堆碎片