读取行和尾随空白行与字符串工作正常,但不是当我尝试读取宽字符串时。 [英] Read Line And Trailing Blank Lines Works Fine With Strings But Not When I Am Trying To Read Wide Strings.

查看:85
本文介绍了读取行和尾随空白行与字符串工作正常,但不是当我尝试读取宽字符串时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="c++">

std::wstring
FDIWTextStream::
ReadLineAndGetTrailingBlankLines(int& trailingBlankLines)
{
    std::wstring    result;
    wchar_t     currentChar;
    const int   bufSize=100;
    wchar_t     buffer[bufSize];

    int savedPosition = mStreamIMP->GetPositionIMP();
    int blankLines = 0;
    wchar_t      newBuff[bufSize];
    for (;;)
    {
        int bytesRead = std::min(bufSize, GetLength() - GetPosition());
        int i;


        ReadBytes(buffer, bytesRead);

                // scan buffer for a return or linefeed
        for (i=0; i<(bytesRead/sizeof(wchar_t)); i++)
        {
            wchar_t character= buffer[i];
            if(character == L'\n' || character == L'\r')
                break;
        }

        // if no return or linefeed in this buffer then continue looking
        if (i == (bytesRead/sizeof(wchar_t)))
        {
            //wcsncpy(const_cast<wchar_t*>(result.c_str()),buffer, i);
            result += std::wstring(buffer,i);

            // if we are at the end then return what we got so far
            if (IsPositionAtEnd())
                break;
        }
        else
        {
            result += std::wstring(buffer,i);
            break;
        }
    }

    // set the position to the end of the string
    mStreamIMP->SetPositionIMP(savedPosition + result.length());

    // go past all returns and linefeeds
    while (!IsPositionAtEnd())
    {

        *this >> currentChar;

        if (currentChar != L'\n' && currentChar != L'\r')
        {
            // went to far, back up
            mStreamIMP->SetPositionIMP(mStreamIMP->GetPositionIMP()-1);
            break;
        }
        else if(currentChar == L'\n')
            blankLines++;
    }

    if(blankLines > 0)
        trailingBlankLines = blankLines - 1;

    return result;
}







void
FDTextStreamTester::
testReadLineAndGetTrailingBlankLines()
{
	std::wstring data = L"\nA\n\r\n\nTest.\n\nHere\nB";
	FDMemObject memFile;
	FDOStream dataWriter(memFile);
	dataWriter.Reset();
	dataWriter << data;
	

	std::wstring result;
	int blankLines = 0;
	FDIWTextStream reader(memFile);
	result = reader.ReadLineAndGetTrailingBlankLines(blankLines);
	assert(blankLines == 0);
	
	result = reader.ReadLineAndGetTrailingBlankLines(blankLines);
	assert(result == L"A");
	assert(blankLines == 2);
	
	result = reader.ReadLineAndGetTrailingBlankLines(blankLines);
	assert(result == L"Test.");
	assert(blankLines == 1);
	
	result = reader.ReadLineAndGetTrailingBlankLines(blankLines);
	assert(result == L"Here");
	assert(blankLines == 0);
	
	result = reader.ReadLineAndGetTrailingBlankLines(blankLines);
	assert(result == L"B");
	assert(blankLines == 0);
}





在缓冲区中,当我读取字节时,我在缓冲区的起始处读取一个垃圾字符以及字符串如图所示.... * \ nA \ n \\\\ nnTest。\ n \ n \\ nHere \ NB

但是当我读取字符串时宽字符串缓冲区的开头是没有垃圾字符的\\\
A \ n \\\\\\\\\\\\\ nnB。

请让我知道为什么我会在字符串的开头添加。

推荐答案

因为正在读取字符位置的位置,如果我们得到奇数数字位置然后字节计数变得混乱。

Bytecount应该始终是偶数,现在位置和长度被操纵以匹配偶数如下: -






//将位置设置为字符串的结尾

* mStreamIMP-> SetPositionIMP(savedPosition + result.length( )* sizeof(wchar_t)); *



//通过所有退货和换行

while(!IsPositionAtEnd())

{

*此>> currentChar;



if(currentChar!= L'\\ n'&& currentChar!= L'\')

{

** //走到远处,备份

* mStreamIMP-> SetPositionIMP(mStreamIMP-> GetPositionIMP() - sizeof(wchar_t) ); *

休息;

}

else if(currentChar == L'\\ n')

blankLines ++;

}
Since position was being read for character position and if we got odd number position then byte count was getting messed .
Bytecount should always be even and now position and length is manipulated to match even numbers as follows:-



// set the position to the end of the string
*mStreamIMP->SetPositionIMP(savedPosition + result.length()*sizeof(wchar_t));*

// go past all returns and linefeeds
while (!IsPositionAtEnd())
{
*this >> currentChar;

if (currentChar != L'\n' && currentChar != L'\r')
{
**// went to far, back up
*mStreamIMP->SetPositionIMP(mStreamIMP->GetPositionIMP() - sizeof(wchar_t));*
break;
}
else if(currentChar == L'\n')
blankLines++;
}


这篇关于读取行和尾随空白行与字符串工作正常,但不是当我尝试读取宽字符串时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆