fstream error: no match for 'operator<<' in 'wrf << "insert into"'|

fstream error: no match for 'operator<<' in 'wrf << "insert into"'|

所以,我正在尝试编写一个程序来生成 SQL 插入命令语句并将它们写入 .txt 文件。对于开始,我只写了一些代码,这些代码只会写一个插入命令的开始:table 名称和列名。

#include <iostream>
#include <iomanip>
#include <stack>
#include <queue>
#include <fstream>'

using namespace std;
ifstream wrf;

int main()
{
    queue<string>row1;
    queue<string>row2;
    queue<string>values;
    // queue<void>storeValues;

    string table;
    int columnVal;
    int valuesVal;
    string insertQ = "insert into";
    string valQ = "values";
    string columnName;

    cout << "Insert table name: ";
    cin >> table;
    cout << "Number of columns: ";
    cin >> columnVal;

    int temp = columnVal;
    cout <<"------------------------------\nStulpeliai:\n";
    //------------------------------
    while(temp)
    {
        cin >> columnName;
        row1.push(columnName);
        temp--;
    }
    //int temp2 = valuesVal;

    wrf.open ("DB.txt");
    cout << "\n------------------------------\nTEST\n";
    cout << insertQ << table << "\n\t(";
    wrf >> insertQ >> table >> "\n\t(";
    while(row1.size() != 1)
    {
        cout  << row1.front() << ", ";
        wrf  >> row1.front() >> ", ";
        row2.push(row1.front());
        row1.pop();
    }

    cout  << row1.front() <<") ";
    wrf  >> row1.front() <<") ";
    row2.push(row1.front());
    row1.pop();

    wrf.close();
    return 0;
}

出于测试原因,我尝试编写 ifstream 句子来测试它如何将其写入 .txt 文件,但是 我遇到了不匹配错误...

有什么想法吗?

P.S。我只是出于学习原因使用队列。我希望问题足够全球化。

wrf 是一个 ifstream 输入流。 您只能在 ifstream, and operator<< on ofstream.

上使用 operator>>

但是您可以使用 fstream 对象,这样您就可以同时执行这两项操作。

如果你想写入wfs,你的代码应该修改为:

ofstream wrf;
// in the definition
// .....

//...
// when outputting to the file 
wrf << insertQ << table << "\n\t(";
while(row1.size() != 1)
{
    cout  << row1.front() << ", ";
    wrf  << row1.front() << ", ";
    row2.push(row1.front());
    row1.pop();
}

cout  << row1.front() <<") ";
wrf  << row1.front() <<") ";
row2.push(row1.front());
row1.pop();

wrf.close();
return 0;
}