技术交流QQ群:1027579432,欢迎你的加入!

#include "iostream"
#include "fstream"
#include "vector"
#include "string"

using namespace std;

// demo1中是向文件中写入string类型,下面是向文件中写入int类型
int cost[10][10];

int main(){
    int v, w, weight;
    ifstream infile;  // 读操作
    ofstream outfile;  // 写操作
    infile.open("E:\\C++\\cpp_Code\\data.txt", ios::in);
    if (! infile.is_open())
        cout << "打开文件失败...." << endl;
    while(!infile.eof()){
        infile >> v >> w >> weight;
        cost[v][w] = weight;
        cost[w][v] = weight;
    }
    infile.close();

    outfile.open("E:\\C++\\cpp_Code\\result.txt", ios::app);
    if(!outfile.is_open())
        cout << "打开文件失败...." << endl;
    for(int i=0;i!=10;++i){
        for(int j=0;j!=10;++j){
            outfile << i << "\t" << j << "\t" << cost[i][j] << endl;
        }
    }
    outfile.close();
    return 0;
}