包含一個fstream的庫,然後定義一個ifstream的物件,比如下面的infile;讀取資料,方陣可以匯入二維陣列中/////////////////////////////////////////////////////////////////////#include<iostream>#include<fstream>#include<math.h>using namespace std;int main(){ int n; double density; ifstream infile("C:\\Users\\administrator\\Desktop\\M_change_V\\M_data_in.txt",ios::in); infile>>density>>n; double mass[n],V[n]; for(int i=0;i<n;i++) infile>>mass[i]; infile.close(); ofstream outfile("C:\\Users\\administrator\\Desktop\\M_change_V\\V_data_out.txt",ios::out); outfile<<"接液體積"<<endl; for(int i=0;i<n;i++) { V[i]=mass[i]/(3*density); outfile<<V[i]<<endl; } outfile.close(); cout<<"計算完成"; return 1;}
包含一個fstream的庫,然後定義一個ifstream的物件,比如下面的infile;讀取資料,方陣可以匯入二維陣列中/////////////////////////////////////////////////////////////////////#include<iostream>#include<fstream>#include<math.h>using namespace std;int main(){ int n; double density; ifstream infile("C:\\Users\\administrator\\Desktop\\M_change_V\\M_data_in.txt",ios::in); infile>>density>>n; double mass[n],V[n]; for(int i=0;i<n;i++) infile>>mass[i]; infile.close(); ofstream outfile("C:\\Users\\administrator\\Desktop\\M_change_V\\V_data_out.txt",ios::out); outfile<<"接液體積"<<endl; for(int i=0;i<n;i++) { V[i]=mass[i]/(3*density); outfile<<V[i]<<endl; } outfile.close(); cout<<"計算完成"; return 1;}