             return count
         }
         if ferror(self.pointer) != 0 {
-            throw FileError.ReadFailed(File.descriptionOfLastError())
+            throw FileError.ReadFailed(Errno.description())
         }
         throw FileError.ReadFailed("Unknown file read error occured.")
     }

    public static func isDirectory(path: String) throws -> Bool {
        var s = stat()
        guard path.withCString({ stat($0, &s) }) == 0 else {
            throw FileError.IsDirectoryFailed(descriptionOfLastError())
        }
        return s.st_mode & S_IFMT == S_IFDIR
    }

        }
        try data.withUnsafeBufferPointer {
            if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
                throw FileError.WriteFailed(Errno.description())
            }
        }
    }
    
    public func seek(offset: Int) throws -> Void {
        if fseek(self.pointer, offset, SEEK_SET) != 0 {
            throw FileError.SeekFailed(Errno.description())
        }
    }

}

public func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result {
    return try withFileOpenedForMode(path, mode: "wb", f)
}

public func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
    return try withFileOpenedForMode(path, mode: "rb", f)
}

public func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
    return try withFileOpenedForMode(path, mode: "r+b", f)
}

public func withFileOpenedForMode<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result {
    let file = try File.openFileForMode(path, mode)
    defer {
        file.close()
    }
    return try f(file)
}

