Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用c+;加载简单文本数据的帮助吗+; 我需要帮助将自定义文件格式加载到我的C++程序中… 我知道有一个简单的方法可以做到这一点,但我想我用的是 在线搜索时使用了错误的术语_C++_File Io_Codeblocks - Fatal编程技术网

需要使用c+;加载简单文本数据的帮助吗+; 我需要帮助将自定义文件格式加载到我的C++程序中… 我知道有一个简单的方法可以做到这一点,但我想我用的是 在线搜索时使用了错误的术语

需要使用c+;加载简单文本数据的帮助吗+; 我需要帮助将自定义文件格式加载到我的C++程序中… 我知道有一个简单的方法可以做到这一点,但我想我用的是 在线搜索时使用了错误的术语,c++,file-io,codeblocks,C++,File Io,Codeblocks,我的3d对象自定义格式如下所示: NumVerts 6 //verts (float) -1 -1 0 1 -1 0 -1 1 0 1 -1 0 1 1 0 -1 1 0 //texture (float) 0 0 1 0 0 1 1 0 1 1 0 1 //index (int) 0 1 2 1 3 2 那是一个四方形。。。(是的,我知道……可怕的格式……但这正是我在安卓游戏中使用的格式) 我想在C++中为我的编辑器(SDL+OpenGL for Windows)加载一个函数,这些文件将这些

我的3d对象自定义格式如下所示:

NumVerts 6
//verts (float)
-1
-1
0
1
-1
0
-1
1
0
1
-1
0
1
1
0
-1
1
0
//texture (float)
0
0
1
0
0
1
1
0
1
1
0
1
//index (int)
0
1
2
1
3
2
那是一个四方形。。。(是的,我知道……可怕的格式……但这正是我在安卓游戏中使用的格式)

<>我想在C++中为我的编辑器(SDL+OpenGL for Windows)加载一个函数,这些文件将这些文件加载到数据中…不幸的是,尽管我知道如何用C++输出这种格式,但我无法理解如何导入它们。我希望使用fstream命令

如果有人能很快写出一个简单的版本,我会非常感激

我只是想做以下几点:

  • 从输入字符串中查找文本文件
  • 读取“NumVerts”并获取在它之后写入的整数
  • 循环下一行(NumVerts*3),并将每个数字作为一个浮点数抓取
  • 循环通过下一行(NumVerts*2),并将每个数字作为一个浮点数抓取
  • 循环通过下一行(NumVerts*1)并获取每个数字作为Int
  • (跳过任何以“/”开头的行)
  • 关闭文件
谢谢你的阅读,任何帮助现在都会很好。。。或者是一个非常简单的relivent链接,它从文件中读取字符串并从中获取数字以放入内存

我真的很想完成这个游戏,但要找到有用的教程真的很有压力


更新:更新了脚本。。。我不小心忘了将1和0分开。

希望这有帮助。顺便说一句,顶点组件的数量不正确-需要
18个

#include <algorithm>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;

int load_3d_object(const std::string& filename,
                   std::vector<float>& vertComponents,
                   std::vector<float>& texComponents,
                   std::vector<int>& indices)
{
    std::ifstream fs(filename.c_str());
    std::string line;
    if(!std::getline(fs, line))
    {
        throw std::runtime_error("The input file is empty");
    }

    if(line.substr(0,8) != "NumVerts")
    {
        throw std::runtime_error("The first line must start with NumVerts");
    }

    // Extract the number of vertices.
    int numVerts = lexical_cast<int>(line.substr(line.find(' ') + 1));

    // Read in the vertex components, texture components and indices.
    while(std::getline(fs, line))
    {
        boost::trim(line);
        if(line.substr(0,2) == "//") continue;

        if((int)vertComponents.size() < numVerts * 3)       vertComponents.push_back(lexical_cast<float>(line));
        else if((int)texComponents.size() < numVerts * 2)   texComponents.push_back(lexical_cast<float>(line));
        else                                                indices.push_back(lexical_cast<int>(line));
    }

    return numVerts;
}

int main()
{
    std::vector<float> vertComponents;
    std::vector<float> texComponents;
    std::vector<int> indices;
    try
    {
        int numVerts = load_3d_object("input.txt", vertComponents, texComponents, indices);
    }
    catch(std::exception& e)
    {
        std::cout << e.what() << '\n';
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::词法转换;
int load_3d_对象(常量std::string和filename,
标准::矢量和垂直分量,
标准::矢量和纹理分量,
标准:向量和索引)
{
std::ifstream fs(filename.c_str());
std::字符串行;
如果(!std::getline(fs,line))
{
抛出std::runtime_错误(“输入文件为空”);
}
if(第0,8行)!=“NumVerts”)
{
throw std::runtime_error(“第一行必须以NumVerts开头”);
}
//提取顶点数。
int numVerts=lexical_cast(line.substr(line.find(“”)+1));
//读入顶点组件、纹理组件和索引。
while(std::getline(fs,line))
{
增压:微调(直线);
如果(行substr(0,2)==“/”)继续;
if((int)vertComponents.size()std::cout希望这能有所帮助。顺便说一句,您的顶点组件数量错误-您需要
18个

#include <algorithm>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;

int load_3d_object(const std::string& filename,
                   std::vector<float>& vertComponents,
                   std::vector<float>& texComponents,
                   std::vector<int>& indices)
{
    std::ifstream fs(filename.c_str());
    std::string line;
    if(!std::getline(fs, line))
    {
        throw std::runtime_error("The input file is empty");
    }

    if(line.substr(0,8) != "NumVerts")
    {
        throw std::runtime_error("The first line must start with NumVerts");
    }

    // Extract the number of vertices.
    int numVerts = lexical_cast<int>(line.substr(line.find(' ') + 1));

    // Read in the vertex components, texture components and indices.
    while(std::getline(fs, line))
    {
        boost::trim(line);
        if(line.substr(0,2) == "//") continue;

        if((int)vertComponents.size() < numVerts * 3)       vertComponents.push_back(lexical_cast<float>(line));
        else if((int)texComponents.size() < numVerts * 2)   texComponents.push_back(lexical_cast<float>(line));
        else                                                indices.push_back(lexical_cast<int>(line));
    }

    return numVerts;
}

int main()
{
    std::vector<float> vertComponents;
    std::vector<float> texComponents;
    std::vector<int> indices;
    try
    {
        int numVerts = load_3d_object("input.txt", vertComponents, texComponents, indices);
    }
    catch(std::exception& e)
    {
        std::cout << e.what() << '\n';
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::词法转换;
int load_3d_对象(常量std::string和filename,
标准::矢量和垂直分量,
标准::矢量和纹理分量,
标准:向量和索引)
{
std::ifstream fs(filename.c_str());
std::字符串行;
如果(!std::getline(fs,line))
{
抛出std::runtime_错误(“输入文件为空”);
}
if(第0,8行)!=“NumVerts”)
{
throw std::runtime_error(“第一行必须以NumVerts开头”);
}
//提取顶点数。
int numVerts=lexical_cast(line.substr(line.find(“”)+1));
//读入顶点组件、纹理组件和索引。
while(std::getline(fs,line))
{
增压:微调(直线);
如果(行substr(0,2)==“/”)继续;
if((int)vertComponents.size()
int\u get\u num\u verts\u值(std::ifstream&a\u in)
{
char-buf[128];
int结果=-1;
a_in.getline(buf,sizeof(buf));
while(a_in.good())
{
如果(a_in.gcount()>9)
{
字符串s(buf);
如果(0==s.find(“NumVerts”))
{
结果=atoi(s.substr(8.c_str());
打破
}
}
a_in.getline(buf,sizeof(buf));
}
返回结果;
}
模板
void _get_值(std::ifstream&a_-in,std::vector&a_值)
{
char-buf[128];
a_in.getline(buf,sizeof(buf));
int i=0;
while(a_in.good())
{
弦线(buf);
如果(0!=行。查找(“/”)
{
a_值[i++]=boost::lexical_cast(行);
//都读了吗?
如果(i==a_values.capacity())
{
打破
}
}
a_in.getline(buf,sizeof(buf));
}
}
int main(int/*a_argc*/,char**/*a_argv*/)
{
ifstream-in(“test.txt”);
const int num_verts_value=\u get_num_verts_value(in);
std::向量顶点(num_verts_值*3);
_通用电气
std::ifstream in("in");
char buf[256];
std::string line;
int NumVerts;
in >> buf >> NumVerts;
std::vector<float> verts(NumVerts * 3);
std::getline(in, line);//read the rest of the line
std::getline(in, line);//skip the comment line
std::for_each(verts.begin(), verts.end(), [&in](float& par){
    in >> par;
});
std::vector<float> texture(NumVerts * 2);
std::getline(in, line);//read the rest of the line
std::getline(in, line);//skip the comment line
std::for_each(texture.begin(), texture.end(), [&in](float& par){
    in >> par;
});
std::vector<int> index(NumVerts);
std::getline(in, line);//read the rest of the line
std::getline(in, line);//skip the comment line
std::for_each(index.begin(), index.end(), [&in](int& par){
    in >> par;
}); 
#include <string>
#include <iostream> // needed for printing stuff out
#include <fstream>  // Needed to read the file
#include <stdexcept>
#include <vector>

using namespace std;

struct Vertice {
    double x,y,z; // 3 part stuff to read
    double a,b;   // 2 part stuff to read
    int i;        // 1 int thing to read
};

/// Reads a number ignoring comment lines (and any other line that isn't a number)
template <typename T>
T readNumber(istream& data) {
    char lastChar;
    while (data.good()) {
        data >> lastChar; // Don't use peek as that won't skip whitespace
        data.putback(lastChar);
        if (( (lastChar >= '0') && (lastChar <= '9') ) || (lastChar == '-')) {
            // If we're looking at a number read and return it
            T result;
            data >> result;
            return result;
        } else {
            // If it's not part of a number .. assume it's a comment line and skip the whole line
            string commentLine;
            getline(data, commentLine);
            // TODO: Maybe just skip '//' lines and throw an exception for everything else..
        }
    }
    throw exception("Couldn't read file");
}

double readDouble(istream& data) { return readNumber<double>(data); }
int readInt(istream& data) { return readNumber<int>(data); }

int main(int argc, char** argv) {
    if (argc != 2) 
        cout << "Usage: " << argv[0] << " [DATA_FILE_NAME]" << endl;
    else {
        fstream data(argv[1], ios_base::in);
        data >> skipws; // Skip whitespace
        string lastString;
        long numVerts = -1;
        // Read in words ignoring everything until we hit a 'NumVerts'
        while (numVerts < 0) {
            data >> lastString;
            if (lastString == "NumVerts")
                data >> numVerts;
        }
        // We know how many to get now
        typedef vector<Vertice> Verts;
        Verts verts(numVerts);
        // Read in the triples
        for (Verts::iterator i=verts.begin(); i != verts.end(); ++i) {
            i->x = readDouble(data);
            i->y = readDouble(data);
            i->z = readDouble(data);
        }
        // Read in the pairs (some other data)
        for (Verts::iterator i=verts.begin(); i != verts.end(); ++i) {
            i->a = readDouble(data);
            i->b = readDouble(data);
        }
        // Read in the single integer value
        for (Verts::iterator i=verts.begin(); i != verts.end(); ++i) {
            i->i = readInt(data);
        }
        // Print out all we found
        for (Verts::iterator i=verts.begin(); i != verts.end(); ++i) {
            cout << "Vertice" << endl
                 << "  x: " << i->x << endl
                 << "  y: " << i->y << endl
                 << "  z: " << i->z << endl
                 << "  a: " << i->a << endl
                 << "  b: " << i->b << endl
                 << "  i: " << i->i << endl
                 << endl;
        }

    }

}