在c ++中修复了一个奇怪的写入文件错误

时间:2010-09-08 02:44:18

标签: c++ file

我遇到了这个非常奇怪的问题,我正在将结果写入输出文件,我使用函数AB和C i按顺序激活它们,文件中的结果以不同的顺序打印,首先来自A比来自C和之后的B.我只是无法理解为什么结果以不同于激活顺序的顺序打印。感谢名单。

代码:

void Manager::AddCommand(Command* com, ofstream &ResultFile)
{
    if (com != NULL)
    {
        if (com->ValidCommand(ResultFile) == true)
            commands.push_back(com);
        else
                delete com;
    }
}

bool Command::ValidCommand(ofstream &Result) const
{
    if(func<PrintCityCouncilList || func >HireEmployee){
        Result << "Command:Failed activating function - invalid function number\n";
        return false;}
    if ((func == Command::PrintDepartmentEmployees) || (func == Command::PrintDepartmentExpenses) || (func == Command::PrintDepartmentStatistics)){
        if(dept<Employee::Engineering ||dept>Employee::Sanitation )
        {
            Result << "Command:Failed activating function - invalid department number\n";
            return false;
        }
    }
    return true;
}

void Manager::ActivateCommand(Command* com, ofstream &Result)
{
    if(com != NULL)
    {
        switch(com->GetFunction())
        {
            case (Command::PrintCityCouncilList): pcc->PrintCityCouncilDetails(Result);
                break;
            case (Command::PrintDepartmentEmployees):ActivatePrintDeprtEmployees(com->GetDepartment(), Result);
                break;
            case (Command::PrintEmployeeSalary):ActivateEmployeeSalary(com->GetId(), Result);
                break;
            case (Command::PrintDepartmentExpenses):ActivateDeprtExpenses(com->GetDepartment(), Result);
                break;
            case (Command::PrintCityCouncilExpenses): pcc->AllExpenses (Result);
                break;
            case (Command::PrintDepartmentStatistics):ActivateDeprtStatistics(com->GetDepartment(), Result);
                break;
            case (Command::FireEmployee): pcc->RemoveEmployeeFromCC(NULL,com->GetId(),Result);
                break;
            case (Command::HireEmployee): pcc->AddEmployeeToCC(com->GetId(),com->GetPrivateName(),com->GetSurName(),com->GetDate(),com->GetAddress(),com->GetDepartment(), com->GetStatus(),com->GetSalary(),com->GetPositionPercent(),com->GetPhoneNum(), Result);
                break;
            default:Result<<"Manager:Failed Activating command - invalid function"<<endl;
                break;
        }

    }
}

void Manager::ActivateCommandsList(ofstream &Result)
{
    Command* tmp = NULL;
    if (commands.empty() == false)
    {
        list<Command*>::iterator iter = commands.begin();
        while (iter != commands.end())
        {
            tmp = (Command*)(*iter);
            ActivateCommand(tmp,Result);
            iter++;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

好的,这是交易,如果你在visual stdio中运行它,你的代码可能会遇到一些困难。你看,当它试图优化你的代码时,它有一些错误。关闭优化。同时刷新您的流,将 endl 放入 cout \ n printf 中完成。还有另一种可能性,就是Stack-Corruption或Heap-Corruption。检查您的动态对象是否在其边界内引用。 只要你不发送代码,就像我的朋友刚才说我们应该咨询晶球。

答案 1 :(得分:0)

我的猜测是,如果这是作业,你就没有线程问题。您是否尝试从A,B和C刷新输出流?这可能会解决您的问题。