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

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

using namespace std;

int cost[10][10];
// 如果源文件中的每行数据数量不一样,demo2中的方法就不行了

int main(){
    int Num_3, Num_2;
    int v, w, weight;
    ifstream infile;  // 读操作,输入流
    ofstream outfile; // 写操作,输出流
    infile.open("E:\\C++\\cpp_Code\\data1.txt", ios::in);
    if (!infile.is_open())
        cout << "打开文件操作失败...\n";
    infile >> Num_3 >> Num_2;  // 先读取第一行
    while(0!=Num_3){  // 读取第3个数据
        infile >> v >> w >> weight;
        cost[v][w] = weight;
        cost[w][v] = weight;
        Num_3--;
    }
    while(0!=Num_2){  // 读取第2个数据
        infile >> v >> w;
        cost[v][w] = 100;
        cost[w][v] = 100;
        Num_2--;
    }
    infile.close();


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