Fscanf problem using code::Blocks and Opengl

Asked

Viewed 474 times

1

Does anyone know if there are any restrictions on using fscanf in projects that use GLUT (Opengl)? I’m trying to do something simple... read a cloud of dots (x, y, z) from a text file, but the fscanf does not get the values correctly... The same code in a console application in code::Blocks works correctly.

Does anyone have any suggestions of what they might have done wrong?

Code:

#include <windows.h>
#ifdef __APPLE__
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main()
{
    int i;
    int v1x;
    int vetor[9]; // 3 points

    FILE *occluded_triangles;

    occluded_triangles = fopen("occluded_triangles.txt", "rt");

    for(i = 0; i < 9; i++)
    {
        fscanf(occluded_triangles, "%d", &v1x);
        vetor[i] = v1x;
    }

    for(i = 0; i < 9; i++)
    {
        printf("%d\n", vetor[i]);
    }

    system("pause");
    return 0;
}
  • I was having a similar problem, and the problem was with the codebloks. I was passing a game I developed in conio2 for openGL, I was using the project configured to work with GLUT in codebloks, until I needed to open a file was all ok. The solution I found was to include the necessary links to use GLUT and thus work without project in codebloks, it worked. I suspected it was some m*** that I was doing, but the same code ran outside the project and did not run inside the project.

3 answers

1

Although your code contains a few #includes of Opengl libraries, it is not doing anything using Opengl. The only library functions it is using are fopen, fscanf, printf and system.

There’s a problem with your call to fopen. You use the way "rt". This mode does not exist. You should probably just use "r". According to the table of this website (that I am copying of this other answer of mine), these are the valid ways:

  • r: Opens a text file for reading. The file must exist before opening.
  • w: Opens a text file for saving. If the file does not exist, it will be created. If it already exists, the previous content will be destroyed.
  • a: Opens a text file for saving. The data will be added at the end of the file ("append"), if it already exists, or a new file will be created in the case of the previously non-existent file.
  • rb: Open a binary file for reading. Same as mode r previous, only that the file is binary.
  • wb: Create a binary file for writing, as in mode w previous, only that the file is binary.
  • ab: Adds binary data at the end of the file, as in mode a previous, only that the file is binary.
  • r+: Opens a text file for reading and writing. The file must exist and can be modified.
  • w+: Creates a text file for reading and writing. If the file exists, the previous content will be destroyed. If it does not exist, it will be created.
  • a+: Opens a text file for writing and reading. Data will be added at the end of the file if it already exists, or a new file will be created in case the file does not previously exist.
  • r+b: Opens a binary file for reading and writing. Same as r+ above, only that the file is binary.
  • w+b: Creates a binary file for reading and writing. The same as w+ above, only that the file is binary.
  • a+b: Adds data or creates a binary file for reading and writing. The same as a+ above, only that the file is binary.

Also, it is possible that your file occluded_triangles.txt may contain something wrong that fscanf fail.

  • The problem with the code is in the fopen! If I put "occluded_triangles = fopen(" occluded_triangles.txt", "rt");", that is, adding the two backslashes in the file’s 'path', the code works. Maybe it’s something related to GLUT, because I never needed the " ". It’s really not doing anything with Opengl. I removed all functions as I noticed that the problem was in reading the file ". txt". Thank you for the comment.

  • rt should not be used. It is not standard.

1

You have to better explain what this point cloud (x, y, z) is and how you want to display such information. I’ll assume you intend to read 9 numbers in the file and show them.

First option: Taking, for example, your txt file (here named "my_file.txt") to be:

1 2 3 
4 5 6 
7 8 9

Just simplify your code:

#include <stdio.h> 

int main(void)
{
    int i; 
    int vetor[9];

    FILE *arquivo;

    arquivo = fopen("meu_arquivo.txt", "r");

    for (i = 0; i < 9; i++)     
        fscanf(arquivo, "%d", &vetor[i]);


    for (i = 0; i < 9; i ++)        
        printf("%d\n", vetor[i]);  


    fclose(arquivo);

    return 0;
}

This code will generate the output :

1
2
3
4
5
6
7
8
9

Take that thing off system("pause"), if the terminal is closing use getchar() for portability reasons. Always close the file with fclose and look at the options of fopen. Also, don’t fill your program with variables for no reason (v1x) or #includes unnecessary. Finally, it would also be prudent to put a conditional test if the pointer to the file returns NULL or not, and only then perform the intended actions based on this test.

Second option: For best result and have fewer lines of code, I advise using the txt file as

1, 2, 3, 
4, 5, 6,
7, 8, 9

and make the code:

#include <stdio.h> 

int main(void)
{
    int i;

    int vetor[9] = {

        #include "meu_arquivo.txt"
    };

    for(i = 0; i < 9; i++)
        printf("%d\n", vetor[i]); 

    return 0;
}

This avoids the use of fscanf, fopen and fclose. It would be ideal for your case.

1

As you yourself discovered, in the comments to one of the answers, the problem is the " " within stringng - this has nothing to do with Opengl, and has little to do with "fopen" - the problem is that Windows uses the same character to separate directories that used as an escape character in C strings.

You should always use two backslashes " " inside strings in C and in other languages to represent a single " ". A simple " " is used as a prefix for special characters, for example: "t" for a character "tab", "n" for a new line, etc...

The problem is not visible in your listing, just in your comment - you better update the listing and put the " " there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.