c++ 积累

2019-10-09  本文已影响0人  张亦风

c++读写文件

#include <fstream>
ofstream         //文件写操作 内存写入存储设备 
ifstream         //文件读操作,存储设备读区到内存中
fstream          //读写操作,对打开的文件可进行读写操作 

写文件

     // writing on a text file
    #include <fiostream.h>
    int main () {
        ofstream out("out.txt");
        if (out.is_open()) 
       {
            out << "This is a line.\n";
            out << "This is another line.\n";
            out.close();
        }
        return 0;
    }

读文件

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    int main () {
        char buffer[256];
        ifstream in("test.txt");
        if (! in.is_open())
        { cout << "Error opening file"; exit (1); }
        while (!in.eof() )
        {
            in.getline (buffer,100);
            cout << buffer << endl;
        }
        return 0;
    }
    //结果 在屏幕上输出
     This is a line.
     This is another line
   //结果: 在out.txt中写入:
   This is a line.
   This is another line 

sudo ln -s /usr/local/cuda-9.1 /usr/local/cuda 建立文件间的链接

上一篇下一篇

猜你喜欢

热点阅读