Problems using fscanf_s instead of fscanf

-2

i have a problem with a render obj model in visual studio, using openGL. I tried using fscanf, but my version of visual studio generate a lot of errors. (Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details). After, i tried to use fscanf_s, but generate this exception (Excepción producida en 0x00007FFA12FAE1F9 (ucrtbased.dll) en GL_framework.exe: 0xC0000005: Infracción de acceso al escribir en la ubicación 0x000000C7C2360000. ocurrió). Thanks everyone, and sorry for my english expression.

enter code here
//NON USING ERRNO_T, FOPEN_S AND FSCANF_S
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#include <glm\gtc\type_ptr.hpp>
#include <glm\gtc\matrix_transform.hpp>


bool loadOBJ(const char * path,
    std::vector < glm::vec3 > & out_vertices,
    std::vector < glm::vec2 > & out_uvs,
    std::vector < glm::vec3 > & out_normals
) {
    std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;
    std::vector< glm::vec3 > temp_vertices;
    std::vector< glm::vec2 > temp_uvs;
    std::vector< glm::vec3 > temp_normals;

    FILE * file = fopen(path, "r");
    if (file == NULL) {
        printf("Impossible to open the file !\n");
        return false;
    }

    while (1) {

        char lineHeader[128];
        // read the first word of the line
        int res = fscanf(file, "%s", lineHeader);
        if (res == EOF)
            break; // EOF = End Of File. Quit the loop.

        if (strcmp(lineHeader, "v") == 0) {
            glm::vec3 vertex;
            fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
            temp_vertices.push_back(vertex);
        }
        else if (strcmp(lineHeader, "vt") == 0) {
            glm::vec2 uv;
            fscanf(file, "%f %f\n", &uv.x, &uv.y);
            temp_uvs.push_back(uv);
        }
        else if (strcmp(lineHeader, "vn") == 0) {
            glm::vec3 normal;
            fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
            temp_normals.push_back(normal);
        }
        else if (strcmp(lineHeader, "f") == 0) {
            std::string vertex1, vertex2, vertex3;
            unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
            int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
            if (matches != 9) {
                printf("File can't be read by our simple parser : ( Try exporting with other options\n");
                return false;
            }
            vertexIndices.push_back(vertexIndex[0]);
            vertexIndices.push_back(vertexIndex[1]);
            vertexIndices.push_back(vertexIndex[2]);
            uvIndices.push_back(uvIndex[0]);
            uvIndices.push_back(uvIndex[1]);
            uvIndices.push_back(uvIndex[2]);
            normalIndices.push_back(normalIndex[0]);
            normalIndices.push_back(normalIndex[1]);
            normalIndices.push_back(normalIndex[2]);
        }

    }

    // For each vertex of each triangle


    for (unsigned int i = 0; i < vertexIndices.size(); i++) {
        unsigned int vertexIndex = vertexIndices[i];
        glm::vec3 vertex = temp_vertices[vertexIndex - 1];
        out_vertices.push_back(vertex);
        unsigned int uvindex = uvIndices[i];
        glm::vec2 uv = temp_uvs[uvindex - 1];
        out_uvs.push_back(uv);
        unsigned int normalIndex = normalIndices[i];
        glm::vec3 normal = temp_normals[normalIndex - 1];
        out_normals.push_back(normal);
    }
    return true;
}
//USING ERRNO_T, FOPEN_S AND FSCANF_S
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <glm\gtc\type_ptr.hpp>
#include <glm\gtc\matrix_transform.hpp>

bool loadOBJ(const char* path, std::vector<glm::vec3> &out_vertex, std::vector<glm::vec2> &out_uvs, std::vector<glm::vec3> &out_normals)
{
    std::vector<unsigned int> vertex_Index, uv_Index, normal_Index;
    std::vector<glm::vec3> temp_vertex; //Temporal vector for save all model vertex
    std::vector<glm::vec2> temp_uvs; //Temporal vector for save model uvs(is a vec2 because uvs are a texture)
    std::vector<glm::vec3> temp_normals; //Temporal vector for save all model normals of all vertex

    FILE *file;
    errno_t err = fopen_s(&file, path, "r");
    if (file == nullptr) {
        std::cout << "Impossible to open the file" << std::endl;
        return false;
    }

    while (1)
    {
        char lineHeader[128];
        int res = fscanf_s(file, "%s", lineHeader); //Read the first word of the line
        if (res == EOF)
            break; //Break loop if EOF
        //If read "V" save all vertex on a temoprary vector variable
        if (strcmp(lineHeader, "v") == 0)
        {
            glm::vec3 vertex;
            fscanf_s(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
            temp_vertex.push_back(vertex);
        }
        //If read "VT" save all vertex on a temoprary vector variable
        else if(strcmp(lineHeader, "vt") == 0)
        {
            glm::vec2 uv;
            fscanf_s(file, "%f %f\n", &uv.x, &uv.y);
            temp_uvs.push_back(uv);
        }
        //If read "VN" save all vertex on a temoprary vector variable
        else if(strcmp(lineHeader, "vn") == 0)
        {
            glm::vec3 normals;
            fscanf_s(file, "%f %f %f\n", &normals.x, &normals.y, &normals.z);
            temp_normals.push_back(normals);
        }
        else if(strcmp(lineHeader, "f") == 0)
        {
            std::string vertex1, vertex2, vertex3;
            unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
            int matches = fscanf_s(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
            if (matches != 9) 
            {
                std::cout << "File can't be read by our simple parser : (Try exporting with other options\n)" << std::endl;
                return false;
            }
            vertex_Index.push_back(vertexIndex[0]);
            vertex_Index.push_back(vertexIndex[1]);
            vertex_Index.push_back(vertexIndex[2]);
            uv_Index.push_back(uvIndex[0]);
            uv_Index.push_back(uvIndex[1]);
            uv_Index.push_back(uvIndex[2]);
            normal_Index.push_back(normalIndex[0]);
            normal_Index.push_back(normalIndex[1]);
            normal_Index.push_back(normalIndex[2]);
        }
    }
    //For each vertex of each triangle
    for (unsigned int i = 0; i < vertex_Index.size(); i++)
    {
        unsigned int vertexIndex = vertex_Index[i]; //Index of vertex position
        glm::vec3 vertex = temp_vertex[vertexIndex - 1]; //vertexIndex - 1 because c++ starts in 0, like this start in 1
        out_vertex.push_back(vertex);//Position of the new vertex

        unsigned int uvIndex = uv_Index[i]; //Index of uv position
        glm::vec2 uv = temp_uvs[uvIndex - 1]; //vertexIndex - 1 because c++ starts in 0, like this start in 1
        out_uvs.push_back(uv);//Position of the new uv

        unsigned int normalIndex = normal_Index[i]; //Index of normal position
        glm::vec3 normal = temp_normals[normalIndex - 1]; //vertexIndex - 1 because c++ starts in 0, like this start in 1
        out_normals.push_back(normal);//Position of the normal vertex

    }
    return true;
}
c++

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0