创建文件并读取其中数据 int 型

来源:互联网 发布:有效市场假说知乎 编辑:程序博客网 时间:2024/06/05 13:21
<pre name="code" class="cpp">#include <iostream>#include <fstream>#include <stdlib.h>#include <string>#include <sstream>


fstream co_file;     co_file.open(CO_FILENAME,ios::in);    if(!co_file)    {  co_file.open(CO_FILENAME,ios::out | ios::trunc);        if (co_file.is_open())        {           // co_file << "x_min,x_max,y_min,y_max.\n"            co_file << coo.x_min << '\n';            co_file << coo.x_max << '\n';            co_file << coo.y_min << '\n';            co_file << coo.y_max << '\n';        } }else    {        //读文件        char buffer[50];        int arr[4];        if(!co_file.is_open())         {            cout << "Error opening file";            exit (1);        }        for(int i = 0; i<4  ;i++)        {            co_file.getline(buffer,50);            stringstream(buffer) >> arr[i];        }        coo.x_min = arr[0];        coo.x_max = arr[1];        coo.y_min = arr[2];        coo.y_max = arr[3];    }

0 0
原创粉丝点击