• C++文件基础知识


    一切皆文件,对象和文件的前世今生是一个怎么样的故事?它们之间有什么样的爱恨情仇?。。。最近在追《国家宝藏》,情不自禁了。

    因为之前对C++文件的相关操作一直没重视,写自己paper idea的时候,“书到用时方恨少”的感觉特别强烈,所以系统地学习了文件的基础知识。下面是自己写的一个读取文件内容,并保存到vector<vector<double>>中的代码。

    #pragma once
    #include<iostream>
    #include<string>
    #include<vector>
    #include<fstream>
    #include<sstream>
    using namespace std;
    struct Point
    {
    vector <double> f;

    };
    int main(void)
    {
    vector<Point> thisLine; //定义一个存放Point类的vector对象
    //打开存放数据的文件
    ifstream reader;//定义一个可以从文件读取数据的对象reader;
    istringstream par; // 定义一个未绑定的可以从string中读取数据的对象par
    string line;//line用于保存整行数据
    double value;//用于保存每行的单个数据
    reader.open("C:/Users/Administrator/Desktop/knee_preference/m1.txt");//打开文件
    if (!reader)//检查文件是否读取成功,养成好的习惯!
    cout << "Error opening results file" << endl;
    else
    {
    //读取整行数据并存到string对象line中
    while (getline(reader, line))
    {

    par.str(line); //将line中数据拷贝到par中

    Point tempData;
    while (par >> value)//将par中的数据输出到value中
    {
    tempData.f.push_back(value);
    }
    thisLine.push_back(tempData);
    par.clear();
    }
    reader.close();
    }
    //输出数据
    for (vector<Point>::iterator it = thisLine.begin(); it != thisLine.end(); ++it)
    {
    for (int i = 0; i < (*it).f.size(); i++)
    {
    cout << (*it).f[i] << " ";
    }
    cout << endl;
    }
    //cout << "Hello" << endl;
    return 0;
    system("pause");
    }

  • 相关阅读:
    JDK自带keytool工具配置HTTPS加密协议
    利用 GOST 搭建加密中转隧道(UDP+TCP)
    Linux登录报错-bash: /etc/profile: Permission denied
    转:Windows server 2008 R2 更新补丁失败进入恢复模式
    转:Windows server 2008R2更新补丁后进入系统恢复
    转:CENTOS创建IP白名单
    centos6 离线升级openssh防止断开
    关闭oracle一直等待
    Java并发容器
    Java对象序列化
  • 原文地址:https://www.cnblogs.com/houdada-cn/p/8016241.html
Copyright © 2020-2023  润新知