C++规范格式读取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OOFFrankDura/article/details/83244581

综述

只是做个备份,最近需要做一些规范化数据读取的操作。

代码

#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
#ifndef N
#define N 1000
#endif
struct Powerpoint
{ 
    int id;
    int x,y; //位置信息
    double weight;
    vector<int> neighbors;
    
};
Powerpoint con[N][N];
vector<Powerpoint> all_info;
void show_point(Powerpoint obj){
    cout <<  obj.id << " | " << obj.x <<" " << obj.y <<" " << obj.weight << endl;
    for (int i = 0; i < obj.neighbors.size(); ++i)
    {
        cout << obj.neighbors[i] << endl; 
    }
    cout << "===================" << endl;
}
void  ReadOffFile(const char *filename)
{       /*
     函数说明: 对文件进行读取,读取的是off文件
     */
    int i;
    FILE *fp;                                         //开始读入文件
    if (!(fp = fopen(filename, "r"))) {
        cout << "无法打开文件" << endl;
        return ;
    }
    int line_count = 0;                               //这个是我读入了几行
    char buffer[1024];
    while (fgets(buffer, 1023, fp)) {
        line_count++;
        char *bufferp = buffer;
        while (isspace(*bufferp)) bufferp++;
            int id;
            double x,y; //空间位置
            double w;
            if (sscanf(bufferp, "%d%lf%lf%lf", &(id), &(x), &(y),&(w)) != 4) {
                cout << "点的信息中,数据量不足(4个)" << endl;
                fclose(fp);
                return ;
            }else{
                Powerpoint temp;
                temp.id = id;
                temp.x = x;
                temp.y = y;
                temp.weight = w;
                all_info.push_back(temp);
                show_point(temp);
                
            }
               
    }
    //判断实际的 面 的数目是否和要求数目一样!
    fclose(fp);
}

int main(void){
    ReadOffFile("a.txt");
}

猜你喜欢

转载自blog.csdn.net/OOFFrankDura/article/details/83244581