Introduction to Programming with c++ 13-7 eof的置位时机

来源:互联网 发布:淘宝小李84 编辑:程序博客网 时间:2024/05/22 14:38

问题

考虑如下的问题,文件内容的复制。

思路

枚举字符的方式实现。
有下面的代码:

int copy_file1( const std::string& src, const std::string& tgt ){    if( src == "" || tgt == "" )    {           std::cerr << "Invalid argumnts!" << std::endl;        return -1;    }    std::ifstream fin;    fin.open( src.c_str() );    if( !fin.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::ofstream fout;    fout.open( tgt.c_str() );    if( !fout.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    char ch;    while( fin.get(ch) ) fout.put(ch);    fin.close();    fout.close();    return 0;}

给出源文件:input.dat

abcd

可得如下的输出文件:output.dat

abcd

但是,如果修改上面的代码为下面的代码:

int copy_file2( const std::string& src, const std::string& tgt ){    if( src == "" || tgt == "" )    {           std::cerr << "Invalid argumnts!" << std::endl;        return -1;    }    std::ifstream fin;    fin.open( src.c_str() );    if( !fin.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::ofstream fout;    fout.open( tgt.c_str() );    if( !fout.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    char ch;    while( !fin.eof() ) // 循环结束的条件修改    {        ch = fin.get();        fout.put(ch);    }    fin.close();    fout.close();    return 0;}

则会得到如下的输出文件:output.dat

abcd

也就是会多一个原本没有的字符出现。这是为什么?我对上面的代码增加了文件指针位置的显示,输出如下图。
文件指针显示

我们可以看到,文件指针的位置和我们的想法一样。初始为0,最后一个字符读完之后是4。但是问题是,文件指针明明已经位于文件结束的位置,为什么循环还是会执行一次?

这就是说到了eof置位的时机,在最后一个字符读取完毕之后,文件指针是位于文件结束的位置。但是如果测试eof函数,还是返回false.只有当试图再次读取数据时,发现在文件结束位置进行读取,此时才会eof函数才会置true

所以,上面的第二段代码之所以会多读入一个非法字符就是因为,最后一个字符读取完毕之后,eof还是false。所以,尝试再次读取,此时eof置true。由于没有判断,直接写入,才导致错误。

第一段代码没错是因为,最后一个字符读取完毕之后。循环的条件里面再次进行了读取,所以置位导致无法读取。

所以,再次强调。eof函数的置位时机并不是当文件指针位于文件结束位置时,而是在文件结束位置进行读取时才会置位。

下面给出全部代码实现

#include <iostream>#include <fstream>#include <string>int copy_file(  const std::string& src,  const std::string& tgt );int copy_file1(  const std::string& src,  const std::string& tgt );int copy_file2(  const std::string& src ,  const std::string& tgt );int main( void ){    std::string input_file_name;    std::string output_file_name;    std::cout << "Enter a source file name: ";    std::cin >> input_file_name;    std::cout << "Enter a target file name: ";    std::cin >> output_file_name;    copy_file2( input_file_name, output_file_name );    std::cout << "Copy Done!" << std::endl;    return 0;}int copy_file( const std::string& src,  const std::string& tgt ){    if( src == "" || tgt == "" )    {           std::cerr << "Invalid argumnts!" << std::endl;        return -1;    }    std::ifstream fin;    fin.open( src.c_str() );    if( !fin.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::ofstream fout;    fout.open( tgt.c_str() );    if( !fout.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::string line;    while( std::getline( fin, line ) )     {        fout << line;        if( !fin.eof() )            fout << std::endl;    }    fin.close();    fout.close();    return 0;}int copy_file1( const std::string& src, const std::string& tgt ){    if( src == "" || tgt == "" )    {           std::cerr << "Invalid argumnts!" << std::endl;        return -1;    }    std::ifstream fin;    fin.open( src.c_str() );    if( !fin.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::ofstream fout;    fout.open( tgt.c_str() );    if( !fout.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    char ch;    while( fin.get(ch) ) fout.put(ch);    fin.close();    fout.close();    return 0;}int copy_file2( const std::string& src, const std::string& tgt ){    if( src == "" || tgt == "" )    {           std::cerr << "Invalid argumnts!" << std::endl;        return -1;    }    std::ifstream fin;    fin.open( src.c_str() );    if( !fin.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    std::ofstream fout;    fout.open( tgt.c_str() );    if( !fout.is_open() )    {        std::cerr << "Can not open the file!" << std::endl;        return -1;    }    while( !fin.eof())     {        std::cout << "file pointer before read : " << fin.tellg() << std::endl;;        char ch = fin.get();        fout.put(ch);        std::cout << "file pointer after read : " << fin.tellg() << std::endl; ;    }    fin.close();    fout.close();    return 0;}
0 0