C和C++操作文件的方法

1.C語言

#include<stdio.h>
#include<math.h> 
int main()
{
    float y[61],o[61];
    FILE *fp;
    fp=fopen("2.txt","w");
    for(i = 0;i<=35;i++)
    {
        fprintf(fp,"%f    ",o[i]);
        fprintf(fp,"%f\n",i*0.2);
    }
    fclose(fp);
    return 0;
}

2.C++

        ofstream outfile;
        outfile.open("data.txt");
        if(outfile.is_open())
            cout<<"file is open!!"<<endl;
        else
            cout<<"failed open the file!"<<endl;
        for(int i = 0;i<50;i++)//產生50個x,y數據
        {
            x[i] = this->Xmin+i*dt;
            rand_num = (rand()%5)/5.0;//隨機擾動
            y[i] = rand_num + this->gety(x[i]);
            cout<<"x[i]="<<x[i];
            cout<<"  y[i]= "<<y[i]<<endl;
            outfile<< x[i]<<"    ";
            outfile<< y[i]<<endl;
        }
        outfile.close();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章