2015-09-17 102 views
2

所以,我试图编写一个程序,生成SQL插入命令句子,并将它们写入.txt文件。开始时,我只写了一小段代码,它只将一个插入开始写入命令​​:表名和列名。fstream error:'wrf <<“中的'operator <<'不匹配''插入''|

#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

如果您想写信给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; 
} 
2

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

但是,您可以使用fstream对象,因此您可以同时执行这两个操作。

+0

的问题是,没有一个运营商的工作。我尝试过,但不是或它们工作,相同的错误,所以当问题的根源在哪里?\ –

+0

你想用'wrf >> insertQ >> table >>“\ n \ t(” ;'? – MokaT

+0

我试图添加一个字符串“insert into”和一个表名。 –