fstream错误:不匹配'运营商<<' in' wrf<< "插入"' |

时间:2015-09-17 10:06:57

标签: c++ fstream

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

#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。我只是出于学习的原因使用队列。我希望问题足够全球化。

2 个答案:

答案 0 :(得分:2)

wrf是输入流的ifstream。 您只能使用ifstream上的operator>>ofstream上的operator<<

但是你可以使用fstream对象,这样你就可以做到。

答案 1 :(得分:2)

如果您要写信至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;
}