回覆列表
-
1 # 使用者6982485992823
-
2 # 使用者8140234220371
/*假設你的資料檔案是d:\ttt.txt
用迴圈把數字讀到陣列a裡面順便顯示出來 */
#include
int main()
{
double a[100];
int i=0;
FILE* fp;
if((fp=fopen("d:\\ttt.txt","r"))==0)
{
printf("無檔案!!\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%lf",&a[i]);
printf("%lf\n",a[i]);
i++;
}
fclose(fp);
return 0;
}
包含一個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;}