简体   繁体   中英

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

So, I am trying to write a program that would generate SQL insert into command sentences and would write them into .txt file. For start I only wrote A little of code that would only write a start of insert into command: table name and names of columns.

#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;
}

For testing reasons I tried to write ifstream sentences to test how it writes it into .txt file but I bumped into that no match error...

Any thoughts?

PS I am using queue just for learning reasons. I hope question is global enough.

wrf is an ifstream for input stream. You can only use operator>> on ifstream , and operator<< on ofstream .

But you can use a fstream object so you can do both.

If you want to write to wfs , your code should be modified to:

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;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM