檔案可順利讀取了 但
想問的是 如何在
讀檔後 可以知道 總共有幾個數寫入矩陣
也就是下面的 n值
如何修改寫法 @@
#include <fstream> // 載入fstream標頭檔
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
float A[8];
int n;
float tmp;
ifstream ifile("test1.txt",ios::in);
if(ifile.is_open())
{
for(int i=0;i<8;i++)
{
ifile >> A[i];
}
}
else
cout << "檔案開啟失敗..." << endl;
n=sizeof(A)/sizeof(A[0]);
cout<<"個數:"<<n<<endl;
cout<<"排序前:"<<endl;
for(int i=0;i<8;i++){
cout<<A[i]<<endl;
}
cout<<"排序後:"<<endl;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(A[j]>A[j+1]){
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
}
}
}
for(int i=0;i<n;i++){
cout<<"A["<<i<<"]="<<A[i]<<endl;
}
cout<<"最大值:"<<A[n-1]<<endl;
cout<<"最小值:"<<A[0]<<endl;
ifile.close();
return 0;
}