欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从一个txt文件中读取数据写入到另一个txt文件。

程序员文章站 2022-03-10 15:23:44
读取时会自动将空格忽略,只读取数字。#include using namespace std;int main(int argc, char ** argv){ int num; long double time; char kg; int a=1, b=2; struct timeval t_start, t_end; long double t; ifstream fin("/home/flk/fea_nu...

读取时会自动将空格忽略,只读取数字。


#include <fstream>
using namespace std;

int main(int argc, char ** argv)
{
    int num;
    long double time;
    char kg;
    int a=1, b=2;
    struct timeval t_start, t_end;
    long double t;
    ifstream fin("/home/flk/fea_num6.txt",ios::app);//


    while (1)
    {
        sleep(1);
        cout.setf(ios::fixed,ios::floatfield);//十进制计数法,不是科学计数法
        cout.precision(2);//保留2位小数
        ofstream fout_num("/home/flk/fn_ext.txt",ios::app);
//        ofstream fout_kg("/home/flk/kg_ext.txt",ios::app);
        ofstream fout_time("/home/flk/time_ext.txt",ios::app);
        if(fin.is_open()&&fout_num.is_open()&&fout_time.is_open())
        {
            cout<<"open scueess"<<endl;
        } else{
            cout<<"open faile"<<endl;
        }
        fin>>num;
        cout<<"num: "<<num<<endl;
        fout_num<<num<<" ";
        fin>>time;
        cout<<"time: "<<time<<endl;
        fout_time<<time<<" ";

        if(fin.eof())
        {
            cout<<"finish"<<endl;
            fin.close();
            fout_time.close();
 //           fout_kg.close();
            fout_num.close();
            return 1;
        }

    }
   return 0;
}



本文地址:https://blog.csdn.net/m0_46345373/article/details/110718444