当前位置:  开发笔记 > 编程语言 > 正文

您在C++中执行以下与文件相关的操作吗?

如何解决《您在C++中执行以下与文件相关的操作吗?》经验,为你挑选了2个好方法。



1> Martin York..:

#include 
int main()
{
    std::ifstream    inputFile("MyFileName")  // Opens a File.

    int  x;
    inputFile >> x; // Reads an integer from a file.

    std::string word;
    inputFile >> word; // Reads a space separated word from a file.

    double y;
    inputFile >> y; // Reads a floating point number from the file.

    // etc..
 } // File AutoMagically closed by going out of scope.

写作

#include 
int main()
{
    std::ofstream    inputFile("MyFileName")  // Opens a File.

    int  x = 5;
    inputFile << x << " "; // Writes an integer to a file then a space.
    inputFile << 5 << " "; // Same Again.

    std::string word("This is a line");
    inputFile << word << "\n"; // Writes a string to a file followed by a newline
                               // Notice the difference between reading and
                               // writing a string.
    inputFile << "Write a string constant to a file\n";

    double y = 15.4;
    inputFile << y << ":"; // Writes a floating point number 
                           // to the file followed by ":".

    // etc..
 } // File AutoMagically closed by going out of scope.



2> Johannes Sch..:

一次全部

{ 
    std::ifstream in("foo.txt"); /* opens for reading */
    std::ofstream out("bar.txt"); /* opens for writing */
    out << in.rdbuf(); /* streams in into out. writing and reading */
} /* closes automatically */

推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有