Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;程序不会在控制台中显示文件_C++_File_Output - Fatal编程技术网

C++ c++;程序不会在控制台中显示文件

C++ c++;程序不会在控制台中显示文件,c++,file,output,C++,File,Output,我一直在尝试打印文件的内容,但是,它不会打印任何内容,只会返回0。我已经检查并再次检查了我的代码,但我找不到任何原因来解释它为什么不起作用。这是我的代码示例 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream infile("test.txt"); string line; if(infi

我一直在尝试打印文件的内容,但是,它不会打印任何内容,只会返回0。我已经检查并再次检查了我的代码,但我找不到任何原因来解释它为什么不起作用。这是我的代码示例

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream infile("test.txt");
    string line;
    if(infile.is_open())
    {
      cout << infile.rdbuf();
    }
    else
    {
      cout << "error" << endl;
    }
    infile.close();
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream-infle(“test.txt”);
弦线;
if(infle.is_open())
{

你可以把它读入一个字符串,然后打印出来

#include <string>
#include <fstream>
#include <streambuf>

void func()
{
  // Read into a buffer.
  std::ifstream t("file.txt");
  std::string str;

  t.seekg(0, std::ios::end);   
  str.reserve(t.tellg());
  t.seekg(0, std::ios::beg);

  // Assign to a string.
  str.assign((std::istreambuf_iterator<char>(t)),
            std::istreambuf_iterator<char>());

  // Print out the string to the console.
  std::cout << str << "\n";
}
#包括
#包括
#包括
void func()
{
//读入缓冲区。
std::ifstream t(“file.txt”);
std::字符串str;
t、 seekg(0,标准::ios::结束);
str.reserve(t.tellg());
t、 seekg(0,std::ios::beg);
//分配给字符串。
str.assign((std::istreambuf_迭代器(t)),
std::istreambuf_迭代器();
//将字符串打印到控制台。

std::难道你从未触发读取缓冲区中的任何内容吗?我的main.cpp与文件位于同一文件夹中。我在macbook上使用xcode。可能是@πάντα的重复ῥεῖ 实际上,为什么原始代码不起作用呢?
std::cout
是一个
std::ostream
,并且
std::ostream::operator我已经删除了我的答案。使用
cout谢谢!我一直在试图读取文件并在控制台中显示内容。@science1324当你找到一个令人满意的答案时,请将它标记为已回答。