I'd like to ask you a question about reading C language file input/output data

Asked 2 years ago, Updated 2 years ago, 92 views

void main(int argc, char** argv)
{
    double *fv;
    int numOfVertex;

    int i;
    FILE *f;

    f = fopen("head_new.txt", "r"); // Open Stream
    fscanf(f, "%d \n", &numOfVertex); // Read the number of vertices
    fv = (double*)malloc(sizeof(double)*numOfVertex); // Assign as dynamic as number of vertices 10113

    For (i = 0; i<numOfVertex; i++) // Read coordinates of vertices
        fscanf(f, "%lf", &fv[i]);

    fclose(f); // Close stream

    printf("%d \n", numOfVertex); // Number of vertices output
    for (i = 0; i < numOfVertex; i+=3) // vertex output
    {
        printf("%-20lf", fv[i]);
        printf("%-20lf", fv[i + 1]);
        printf("%-20lf \n", fv[i + 2]);
    }

}

Hello, everyone I asked you a similar question last time and solved it, but this time, I brought a similar source to ask you a question in the source dimension.Please understand that I am asking you questions because of the wrong coding.

What I'm going to do is I'm going to read the data in the notepad and make sure it's read well. In the notepad, the number of vertices and (x,y,z) coordinates of vertices, the number of faces and the number of vertices of that face?They are saved. When you express the data stored in Notepad in OpenGL, you get a human-shaped object. So first, it's in the process of reading and checking the vertices of the notepad.

In the first picture, 10113 is the number of vertices and the real numbers below are the coordinates of vertices. Also, in the next picture starting from 20222, 2022 is the number of noodles, and the numbers below it are the number of vertices. The last picture is the execution result of the sauce I made. The data for these vertices and faces are all stored in one notebook.

The file input/output function was first used to read the coordinates of the vertices in the Notepad. But in the last picture, it looks good, but when I actually checked it, it ended up with some data input.It's way too far. I don't know what's wrong.

In order to receive the data properly, we first receive the information of 10113 in the numOfVertex variable It's assigned as dynamic as 10113. So that's 10113 sequences, and then the for moon Reads data one by one in the dynamically assigned fv variable.

And I print it out below to see if it's read well, but it comes out like that. To make it easier to check, I wrote i+=3 for the conditional expression for the sentence This doesn't seem to be a problem, but I'm not sure why I can't take all the input.

I'm still a beginner, so honestly, I'm not sure if that's wrong.Please A little bit. I'd appreciate your help. I have to print out the number of noodles and the number information of the noodles I'm frustrated that I haven't been able to print out the peak information properly yet

c opengl

2022-09-22 19:32

1 Answers

A vertex has three coordinates (x, y, z), so before dynamic assignment

fscanf(f, "%d \n", &numOfVertex);
numOfVertex *= 3;

Change it to that


2022-09-22 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.