C++出現 error: no match for 'operator>>' (operand types are 'std::ofstream {aka std::basic_ofstream }' and 'char')


從CSV文件中讀取數據代碼:

bool speechManager::fileIsEmpty()
{
    //創建文件流
    ofstream ifs(FILENAME, ios::in);
    //判斷文件是否打開成功
    if(!ifs.is_open())
    {
        cout << "open false" << endl;
        return false;
    }
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        //文件為空
        cout << "no records" << endl;
        ifs.close();
        return false;
    }
    //讀取記錄
    ifs.seekg(0, std::ios::beg);
    int count = 0; //記錄總共記錄有多少條
    vector<string> vec; //按行讀取每一屆記錄,有幾屆就有幾個string
    string str; //每一行讀取的數據
    while(getline(ifs, str))
    {
        vec.push_back(str);
        count++;
    }

    //關閉文件
    ifs.close();
   return true;
}

運行報錯:

error: no match for 'operator>>' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and 'char') ifs >> ch;

分析和解決:

上面的代碼是用ofstream打開文件,即寫入的方式,但是后面卻用它來讀取文件ifs >> ch,將ofstream換成輸入流ifstream即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM