C++中的文件读写操作(3)

技术交流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;   
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容