为什么在ifstream文件中的换行符-由该代码读取时-占用2个字节? [英] Why does the newline character in an ifstream file - when read by this code - occupy 2 bytes?

查看:97
本文介绍了为什么在ifstream文件中的换行符-由该代码读取时-占用2个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个文件,该文件有15行,每个文件包含2个字符,因此假定文件的大小约为44个字节,但是使用tellg函数,该文件的大小显示为58.此外,我累积了所有数组代码在识别换行符的位置,它们都是连续的,因此证实了这一疑问.谢谢!

I used a file which had 15 lines with 2 characters each and hence assumed the size of the file to be around 44 bytes but, using the tellg function, the size is shown as 58. Furthermore, I accumulated an array of all the positions the code was identifying a newline character and they were all consecutive and hence confirmed this doubt. Thank you!

//Tailfile - This program accepts a file and prints the last 10 lines.
//This function determines the number of lines and how to display it
int lineidentifier(fstream&tailfile,long& position)
{
    tailfile.seekg(0,ios::end);//sets the read position at the end of file.
    long n=0;//counter for the number of lines
    long i=tailfile.tellg();//counter for the number of characters set to 
                        //thenumber of bytes in the file and hence, the end.
    char ch;//To hold and check the character.
    while(n<10&&i>=0)//conditions are as long as the number of characters 
                 //are not exhausted or the number of lines
    {
        tailfile.seekg(i, ios::beg);//sets the read position to the end of 
                   //the file by using the number of characters and the file
                                //mode as the beginning.
        cout<<"1. "<<i<<endl;//DEBUGGING EXTRA
        tailfile.get(ch);//Reads the content at i
        tailfile.clear();//clears the eof flag set by the first iteration 
                          //because we reach the end of the file.
        cout<<"2. "<<i<<endl;//DEBUGGING EXTRA
        if(ch=='\n')//if the character received is the newline character 
                 //leading to us regarding it as a line has been identified.
        {
            n++;//Increment n accordingly.
            position=i;//The position is the byte i is at before the 
                 //character was read, hence the position of the character.
            cout<<position<<endl;//DEBUGGING EXTRA
            cout<<ch<<endl;//DEBUGGING EXTRA
            i--;
        }
        i--;
        cout<<"4. "<<i<<endl;//DEBUGGING EXTRA
    }
    cout<<i<<endl;//DEBUGGING EXTRA
    if(i<=1)//Using the position of i to indicate whether the file has more 
         //than 10 lines. If i is less than 1, it has reached the
    //beginning of the file
        return 0;
    else
        return 1;
}

推荐答案

Linux使用 \ n (换行符,0x0A)作为新的换行符.

Linux uses \n (Line Feed, 0x0A) as its new line character.

Windows/DOS使用 \ r \ n (回车(0x0D)和换行(0x0A))作为新的换行字符.

Windows/DOS uses \r\n (Carriage Return (0x0D) and Line Feed (0x0A)) as its new line character.

就像您正在读取DOS编码的文件一样.

Likely you are reading a DOS-encoded file.

此答案提供了更多详细信息

这篇关于为什么在ifstream文件中的换行符-由该代码读取时-占用2个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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