C++寫的小工具

1.讀取一個文件夾下固定格式的文件名,並改名爲序號開頭的文件

//讀取一個文件夾下固定格式的文件名,並改名爲序號開頭的文件
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <iostream>
#include <io.h>
#include <sstream>
using namespace std;

void getFiles(string path, string exd, vector<string>& files)
{
    //文件句柄
    long   hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string pathName, exdName;

    if (0 != strcmp(exd.c_str(), ""))
    {
        exdName = "\\*." + exd;
    }
    else
    {
        exdName = "\\*";
    }

    if ((hFile = _findfirst(pathName.assign(path).append(exdName).c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是文件夾中仍有文件夾,迭代之
            //如果不是,加入列表
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(pathName.assign(path).append("\\").append(fileinfo.name), exd, files);
            }
            else
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    files.push_back((fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

void main(){
    string filePath, extraName;
    cout << "filepath:" << endl;
    cin >> filePath;
    cout << "extra name:" << endl;
    cin >> extraName;
    //filePath = "D:\\sample";
    //extraName = "ply";
    vector<string> files;

    ////獲取該路徑下的所有文件  
    getFiles(filePath, extraName, files);

    char str[30];
    int size = files.size();


    for (int i = 0; i < size; i++)
    {
        std::stringstream sstream;
        std::string Name;
        string newName;
        string newFileName, oldFileName;

        sstream << i;
        sstream >> Name;
        newName = Name.append(".").append(extraName);
        std::cout << files[i] << endl;

        oldFileName = filePath + ("\\") + (files[i]);
        newFileName = filePath + ("\\") + (newName);
        // 改名字
        if (rename(oldFileName.c_str(), newFileName.c_str()) == 0)
        {
            printf("  改名成功完成!!");
        }
    }
    system("pause");
}

2.從obj模型文件中讀取頂點生成點暈(可用來從文件中讀入參數)

#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/common/common.h>
#include <iostream>

int main()
{
    ////create a point cloud pointer and read ply file
    /*pcl::PointCloud<pcl::PointXYZRGB>::Ptr m_cloud (new pcl::PointCloud<pcl::PointXYZRGB>);

    pcl::io::loadPCDFile<pcl::PointXYZRGB>("section.pcd", *m_cloud);*/

    // 讀取obj文件
    ifstream inf;
    inf.open(".\\all\\Merged mesh.obj");

    // 逐行讀取,保存
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr m_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    std::string s, s1, s2, s3, s4;
    while (std::getline(inf, s))
    {
        std::istringstream in(s);
        in >> s1 >> s2 >> s3 >> s4;
        if (s[0] == 'v')
        {
            if (s[1] != 'n'&&s[1] != 't')
            {
                // v開頭的行
                pcl::PointXYZRGB point3D;
                point3D.x = (float)atof(s2.c_str());
                point3D.y = -(float)atof(s4.c_str());
                point3D.z = (float)atof(s3.c_str());
                point3D.r = 0;
                point3D.g = 0;
                point3D.b = 0;

                m_cloud->push_back(point3D);
                //cout << s<<endl;
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章