首页 \ 问答 \ 如何打印文件的内容?(How do I print out the contents of a file? C++ File Stream)

如何打印文件的内容?(How do I print out the contents of a file? C++ File Stream)

我正在使用fstream和C ++,我希望我的程序要做的就是向终端打印我的.txt文件的内容。 这可能很简单,但我在网上看了很多东西,找不到任何可以帮助我的东西。 我怎样才能做到这一点? 这是我迄今为止的代码:

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

int main() {
    string output;
    ifstream myfile;
    ofstream myfile2;

    string STRING;
    myfile.open ("/Volumes/LFARLEIGH/Lucas.txt");

    myfile2 << "Lucas, It Worked";

        myfile >> STRING;
        cout << STRING << endl;
    myfile.close();


    return 0;
}

先谢谢你。 如果这很简单,请原谅我,因为我对C ++很陌生


I am using fstream and C++ and all I want my program to do is to print out to the terminal the contents of my .txt file. It may be simple, but I have looked at many things on the web and I can't find anything that will help me. How can I do this? Here is the code I have so far:

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

int main() {
    string output;
    ifstream myfile;
    ofstream myfile2;

    string STRING;
    myfile.open ("/Volumes/LFARLEIGH/Lucas.txt");

    myfile2 << "Lucas, It Worked";

        myfile >> STRING;
        cout << STRING << endl;
    myfile.close();


    return 0;
}

Thank you in advance. Please forgive me if this is very simple as I quite new to C++


原文:https://stackoverflow.com/questions/35201919
更新时间:2020-10-25 14:10

最满意答案

当这个功能已经在标准的C ++库中实现的时候,没有必要在这里重新发明轮子。

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream f("file.txt");

    if (f.is_open())
        std::cout << f.rdbuf();
}

There's no reason to reinvent the wheel here, when this functionality is already implemented in the standard C++ library.

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream f("file.txt");

    if (f.is_open())
        std::cout << f.rdbuf();
}
2016-02-04

相关问答

更多

如何将一个文件的内容写入另一个文件?(How to write contents of one file to another file?)

using (FileStream stream = File.OpenRead("C:\\file1.txt")) using (FileStream writeStream = File.OpenWrite("D:\\file2.txt")) { BinaryReader reader = new BinaryReader(stream); BinaryWriter writer = new BinaryWriter(writeStream); ...

将内容从C FILE传输到C ++流[关闭](Transfer contents from C FILE to C++ stream [closed])

先关闭它。 fclose(fp); 然后再打开 string line; ifstream myfile ("whatever.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } Close it first. fclose(fp); Th ...

如何打印出矢量的内容?(How to print out the contents of a vector?)

纯粹回答你的问题,你可以使用迭代器: std::vector<char> path; // ... for (std::vector<char>::const_iterator i = path.begin(); i != path.end(); ++i) std::cout << *i << ' '; 如果要在for循环中修改向量的内容,则使用iterator而不是const_iterator 。 但是还有很多可以这么说的。 如果你只想要一个可以使用的答案,那么你可以在这里停下来 否则 ...

PHP文件打印自己的内容(PHP file print its own content)

是。 <?php readfile(__FILE__) __FILE__是一个魔术常量 ,它包含文件的绝对文件系统路径,而readfile只读取和打印内容。 如果你想有一个语法突出的HTML输出,请尝试使用highlight_file函数或highlight_string函数 。 Yes. <?php readfile(__FILE__) __FILE__ is a magic constant that contains the absolute filesystem path to the ...

Perl不打印文件内容(Perl not printing file contents)

当您打开附加文件时,文件末尾会留下文件偏移量,因此阅读将立即让您立即获得。 你需要向后seek (可能你想要文件的开头),然后阅读会得到任何东西。 由于你根本就没有写作,你应该只打开文件进行阅读(正如其他人所说) When you opening for appending that leaves the file offset at the end of the file, so reading will get you an immediate eof. You need to seek ba ...

如何让Python打印文件的内容[重复](How do get Python to print the contents of a file [duplicate])

open为您提供了一个迭代器,它不会立即自动加载整个文件。 它逐行迭代,因此您可以像这样编写一个循环: for line in log: print(line) 如果您只想将文件内容打印到屏幕,则可以使用print(log.read()) open gives you an iterator that doesn't automatically load the whole file at once. It iterates by line so you can write a loop ...

当我在另一个目录中打开文件时,它不会打印文件的内容,而是打印目录?(When I open a file in another directory it won't print the contents of the file but the directory?)

您忘记使用正确的方法来读取文件内容。 file = open(os.path.join(direct, "Test.txt"),'r') print(file.read()) You forgot to use the proper method to read file content. file = open(os.path.join(direct, "Test.txt"),'r') print(file.read())

bash打印转义的文件内容(bash print escaped file contents)

要仅执行您明确要求的两个文字转换: IFS= read -r -d '' content <file content=${content//'"'/'\"'/} content=${content//$'\n'/'\n'} echo "file contents: $content" 也就是说,如果您试图将任意内容表示为JSON字符串,请让一个完全兼容的JSON解析器/生成器完成繁重的工作: IFS= read -r -d '' content <file echo "file contents ...

打印文件的内容 - python套接字(print contents of file - python socket)

您正在获取文件的内容但它是空的...并且您的代码只是因为您可能在同一目录中运行它。 您的客户端打开要写入的文件(然后它是空的)并且服务器读取它(两个代码都使用相同的文件名)。 您可能想从C:\读取文件,因此您应该更改服务器代码并替换行: with open(dataFile, 'rb') as file_to_send: 有: with open('C:\\' + dataFile, 'rb') as file_to_send: You are getting the content of t ...

如何打印文件的内容?(How do I print out the contents of a file? C++ File Stream)

当这个功能已经在标准的C ++库中实现的时候,没有必要在这里重新发明轮子。 #include <iostream> #include <fstream> int main() { std::ifstream f("file.txt"); if (f.is_open()) std::cout << f.rdbuf(); } There's no reason to reinvent the wheel here, when this functionality ...

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)