0

FILEタイプの一種のラッパーであるFileクラスを作成し、いくつかのメソッドを追加しました。

これは私のファイルクラスのコードです:

          #include <Fs/File.h>



File::File(Path& p):
    m_path(p),
    m_openned(false)
{
}

int File::open(const string& mode)
{
    m_f = new FILE;
    fopen(m_path, mode.c_str());
    if (m_f == NULL)
    {
        m_openned = false;
        return -1;
    }
    m_openned = true;
    return 0;
}

bool File::exists()
{
    FILE* file;

    if (file = fopen(m_path, "r"))
    {
        fclose(file);
        return true;
    }

    fclose(file);
    return false;
}

int File::flush(){
    return fflush(m_f);
}

int File::remove()
{
    return ::remove(m_path);
}

int File::close()
{
    if (isOpenned())
    {
        m_openned = false;
        return fclose(m_f);
    }

    return 0;
}

long File::getSize()
{
    struct stat file_status;
    if(!this->exists())
        return -1;
    if (stat(m_path, &file_status) < 0)
    {
        return -1;
    }

    return file_status.st_size;
}

FileMode File::getMode()
{
    struct stat file_status;

    if (stat(m_path, &file_status) < 0)
    {
        return FileMode(-1);
    }

    return FileMode(file_status.st_mode);
}



Path File::getPath()
{
    return m_path;
}


bool File::isOpenned()
{
    return m_openned;
}


int File::setMode(FileMode& mode)
{
    return chmod(m_path, mode);
}

int File::renameTo(File& f)
{
    if (f.exists() || !this->exists())
        return -1;

    return rename( m_path , f.getPath());
}

int File::copyTo(File& to)
{
    char ch;
    this->close();
    this->open(FileTypes::READ);
    to.close();
    to.open(FileTypes::WRITE);

    while (!this->eof())
    {
        ch = this->readc();

        if (ch == -1)
            return 0;

        if (!to.eof())
            to.writec(ch);
    }

    if (this->close() < 0)
    {
        return -1;
    }

    if (to.close() < 0)
    {
        return -1;
    }

    return 0;
}

int File::readc()
{
    if (!isOpenned())
        return FileTypes::ENDOFFILE;

    char c = fgetc(m_f);

    if (ferror(m_f))
    {
        return FileTypes::ENDOFFILE;
    }

    return c;
}

int File::writec(char c)
{
    if (!isOpenned())
        return -1;

    fputc(c, m_f);

    if (ferror(m_f))
    {
        return FileTypes::ENDOFFILE;
    }

    return 0;
}

bool File::eof()
{
    if (!isOpenned())
        return true;

    return feof(m_f);
}

私はいくつかのテストをしました、そして私はある種の問題を抱えています

            Path p1("test.txt");
    Path p2("temp.txt");

    File f1(p1);
    File f2(p2);

    assert(f1.open(FileTypes::READ) == 0);
    assert(f1.exists() == true);
    assert(f1.close() == 0);

    cout<<"Mode of f1 "<<f1.getMode().getStringMode()<<endl;
    cout<<"Path of f1 "<<f1.getPath().getAbsolutePath()<<endl;
    cout<<"Size of f1 "<<f1.getSize()<<endl;

    assert(f2.exists() == false);
    assert(f1.copyTo(f2) == 0);
            //#####################################
             // If I comment f2.close() the                              
             // assert(f1.getSize() == f2.getSize()) test fails and                  
             // f2.getSize() == 0
             ##########################################
    f2.close();

    assert(f2.exists() == true);
    assert(f1.getSize() == f2.getSize());

copyToメソッドでcloseを実行したため、このf2.closeが必要な理由がわかりませんでした。誰かが私を助けることができますか?前もって感謝します。ベン

4

3 に答える 3

0

fcloseストリームをフラッシュします。私の推測では、ファイルを閉じないと、ストリームが完全に書き込まれていないため、サイズが異なります。fflush(to);メソッドの最後に追加して、copyToすべてが書き込まれていることを確認することを検討してください。

于 2011-10-23T22:28:49.953 に答える
0

File::copyTo

    if (ch == -1)
        return 0;

ファイルを適切に閉じずに関数からジャンプします。ターゲットファイルが閉じられていない場合、その内容はおそらくOSに送信されず、後で偽のファイルサイズが報告されます。

于 2011-10-23T22:31:23.983 に答える
0

copyTo関数から複数の出口がありますが、実際にファイルを閉じることを保証するものではありません。copyTo関数を早期に終了している可能性があり、意図したクローズが実行されていないように見えます

 while (!this->eof())
{
    ch = this->readc();

    if (ch == -1)
        return 0;

    if (!to.eof())
        to.writec(ch);
}

ファイルの終わりに到達すると、EOFが取得されます。これは私のOS(Windows)では-1であり、ここで0を返し、クローズ呼び出しをスキップします。

于 2011-10-23T22:35:57.583 に答える