下面的代码应该读取测试字符串然后打开文件并在文本文件中找到类似的字符串(包含多行),通过读取前8个字符以确保代码在正确的行上,然后读取接下来的4个字符,这些字符可能不同每一次。
代码适用于前8个字符,但最后4个字符从不读取
void test_value2()
{
char charRead;
unsigned char test2String[65] = "12345f67895f";
unsigned char comparetest2String[65] = { 0 };
unsigned int counter1 = 0, countChars = 0, countTestChars = 0, countChars2 = 0;
std::fstream testResultsFile;
testResultsFile.open("C:\\Tan\\test.txt", ios::in);
do
{
counter1++; //count number of chars in test2String
} while (test2String[counter1] != '\0');
cout << "number of chars in test2String " << counter1 << endl;
if (!testResultsFile)
{
cout << "File not found " << endl;
cout << "Press ENTER to EXIT " << endl;
getchar();
exit(0);
}
else
{
while (!testResultsFile.eof())
{
testResultsFile.get(charRead); // Read char from the file
countChars++; // count total character read for future comparison with countTestChars
if (countTestChars == 8)
{
countChars2 = countChars;
countTestChars++;
}
if ((charRead == test2String[countTestChars]) && (countTestChars < 9))
{
comparetest2String[countTestChars] = charRead;
countTestChars++;
}
if ((countTestChars > 8) && (countTestChars < counter1)) // if at least first 8 chars matched, keep reading string
{
cout << "trying to read again" << endl;
comparetest2String[countTestChars] = charRead;
countTestChars++;
if (countTestChars == counter1)
{
cout << "done " << endl;
cout << comparetest2String << endl;
break;
}
}
}
}
}
答案 0 :(得分:2)
逻辑错误在以下块中:
if (countTestChars == counter1)
{
cout << "done " << endl;
cout << comparetest2String << endl;
// This line here causes the code to break
// out of the while loop after 8 characters are read.
break;
}
<强>更新强>
在进一步检查代码时,问题似乎出现在以下块中。
if (countTestChars == 8)
{
countChars2 = countChars;
// This line is the culprit. You end up skipping an index.
// Since comparetest2String is zero initialized, you don't
// rest of comparetest2String when you print it.
countTestChars++;
}
删除上述行可解决问题。
请参阅http://ideone.com/b6ebWu处的工作代码。
答案 1 :(得分:1)
您的索引逻辑已关闭,因此您正在跳过索引8.这尤其成问题,因为您对字符数组进行了零初始化。这意味着数组中的任何间隙都将被视为终止字符,字符数组的长度将显示为8.其他字符正在读取并存储在数组中,但它们位于空字符之后。逻辑稍微偏离,所以到目前为止,最后的“f”都没有被读出来。
您的数组comparetest2String
最终看起来像这样:
[1][2][3][4][5][f][6][7][\0][8][9][5]
所以当你打印出来时,你得到:
12345f67
请注意,其他字符将被读取并存储在数组中(减去最后一个'f'),但字符串由'\0'
终止,因此不会打印尾随字符。
您可以尝试从第三个countTestChars
块中的索引if
中减去1吗?这有帮助吗?
std::string
的解决方案如果将其更改为以下内容,该怎么办:
bool checkPrefix(const std::string& test, const std::string& input)
{
const size_t PREFIX_LENGTH = 8;
for (size_t i = 0; i < PREFIX_LENGTH; ++i)
{
if (input[i] != test[i])
{
return false;
}
}
return true;
}
void test_value1()
{
const std::string TEST_STRING = "12345f67895f";
std::cout << "Number of chars in test string" << TEST_STRING.length() << std::endl;
std::ifstream testFile("test.txt");
if (!testFile)
{
std::cout << "File not found " << std::endl;
std::cout << "Press ENTER to EXIT " << std::endl;
std::getchar();
exit(0);
}
else
{
std::string line;
while (getline(testFile, line))
{
if (checkPrefix(TEST_STRING, line))
{
std::cout << "Found " << line << std::endl;
}
}
}
}
这适用于OP所述的多个输入行,不包含任何原始字符数组或容易出错的索引和计数器!
输入:
12345f67895f
BADPREFIX333
12345f67FDBC
输出:
12345f67895f
12345f67FDBC