2013-06-25 136 views
0

我想编写一个程序,它允许用户写一些随机的东西,但我得到了一个 错误,说文件处理C++错误

 no matching call to
,我无法弄清楚。请帮帮我。 当你试图回答这个问题时,尝试更具体的noob。

这里是我的代码

#include<iostream> 
#include<fstream> 
#include<string> 

using namespace std; 

int main() 
{ 
    string story; 
    ofstream theFile; 
theFile.open("Random.txt"); 
while(cin.get(story,5000)!=EOF) 
{ 
    theFile<< story; 
} 
return 0; 
} 
+0

什么是错误你有? –

+0

没有匹配调用“std :: basic_istream :: get(std :) :) and more” –

+1

检查[文档](http://en.cppreference.com/w/cpp/io/basic_istream/get) - 使用'std :: string'的'istream :: get'没有重载。 – jrok

回答

1

cin.get以2个参数预计char*作为第一个参数,你试图传递string作为第一个参数。

如果你想读std::string而不是C字符串直到一行的末尾使用getline(cin, story)

如果你想读的字符串直到下一个空格或换行或其他空白符号使用cin >> story;

1

你似乎试图将cin的内容写入文件。你可以只使用流运营商:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
    string story; 
    ofstream theFile; 
    theFile.open("Random.txt"); 

    if(cin >> story) 
    { 
    theFile << story.substr(0, 5000); 
    } 

    return 0; 
} 

我假设你只是想在Random.txt第5000个字符...