【问题标题】:Reading values from file stream and storing them into a variable size array - OpenGL and C从文件流中读取值并将它们存储到可变大小的数组中 - OpenGL 和 C
【发布时间】:2009-11-28 16:40:25
【问题描述】:

我正在尝试读取以下文件(原始文件中没有 cmets):

Tetra.tri


4 4 // 顶点总数和三角形总数

0.693361 0.693361 0.693361 //顶点坐标

0.693361 -0.693361 -0.693361

-0.693361 -0.693361 0.693361

-0.693361 0.693361 -0.693361

3 1 2 3 // 要显示的三角形(前面的 3 表示是三角形)

3 0 3 2

3 0 1 3

3 0 2 1


我正在尝试使用动态数组来执行此操作,因为我需要打开其他文件并且因为我正在使用顶点数组来绘制到屏幕。

我得到了正确的第一个值(根据上面的文件,vertcount 和 tricount 分别为 4 和 4),但之后我做错了。

代码如下:


void main(){

struct Vertex           // Vertex Structure
{
    float x,y,z;
};

struct Triangle         // Triangle Structure
{
    struct Vertex vert1, vert2, vert3;
};

int vertcount=0;                    //total number of vertices
int tricount=0;                     // number of triangles to display
int v=0;                                //var to store index value of each vertex
int t=0;                                //var to store index value of each triangle
struct Vertex InstVertex;           // Instantiation of Vertex defined as struct with 3 floats to store coordinates
struct Triangle InstTriangle;       // Instantiation of the Triangle STRUCT
long filesize;
char buffer;

struct Vertex vertArray[v];
struct Triangle triArray[t];

FILE * pmesh;                       // pointer to the mesh file to be opened
pmesh = fopen ("/Users/.../tetra.tri","r");             // Tries to access the file specified. TO BE CHANGED ----> Dialaog window with browse for file function

/********** 读取文件并存储值 ************/

fscanf(pmesh, " %i %i ", &vertcount, &tricount);        //read from file and assign the first two values: number of Vertices and Triangles      

vertArray[v] = malloc(vertcount*(sizeof(struct InstVertex)));       // Array of vertexes - space allocated = total number of vertices * the size of each Vertex
triArray[t] = malloc(tricount*(sizeof(struct InstTriangle)));       // Array of triangles

int i=0, j=0;       // Temp variables for for loops

for (i=0; i<=vertcount; i++){
    fscanf(pmesh, "%d %d %d", &InstVertex.x, &InstVertex.y, &InstVertex.z);     //read file and store coordinates in
    vertArray[v]=InstVertex;
    v++;
}   

int check=0;

for (j=0; j<=tricount; j++){
    fscanf(pmesh, "%i %i %i %i", &check, &InstTriangle.vert1, &InstTriangle.vert2, &InstTriangle.vert3);
    triArray[t]=InstTriangle;
    t++;
}

fclose(pmesh);

/****************************************/

glutMainLoop();
return 0;

}


在我犯的错误中,还有数组内存的分配方式,因为我没有得到正确的值。

我在读取循环和数组声明中找不到错误。 这也是在数组中存储值的正确方法吗?

提前谢谢你, 瓦莱里奥

【问题讨论】:

    标签: c arrays file-io filestream


    【解决方案1】:

    我已经解决了读取数组并将值分配给数组的问题,方法是将第一个 for 循环中读取的值从 %d 更改为浮点数 (%f),这给了我正确的值。

    尽管将 malloc 调用更改为:

    vertArray[v] = (struct Vertex) malloc(vertcount*(sizeof(struct Vertex)));
    
    triArray[t] = (struct Triangle) malloc(tricount*(sizeof(struct Triangle)));
    

    其中 v 和 t 已经有值(在本例中为 4 和 4),因为我已经编辑了上面的代码以包含两个调用:

    v=vertcount;
    t=tricount;
    

    但我仍然收到以下错误:

    错误:请求转换为非标量类型!

    有什么想法吗?

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多