Problem with fscanf using code :: blocks and OpenGL

1

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

Does anyone have any suggestions for what I may 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;
}
    
asked by anonymous 03.02.2015 / 22:21

3 answers

1

Although your code contains some% s of OpenGL libraries, it is not doing anything that uses OpenGL. The only library functions that it is using are #include , fopen , fscanf and printf .

There is a problem with your call to system . You use fopen mode. This mode does not exist. You should probably only use "rt" . According to the table of this site (which I am copying this other answer of mine ), these are the valid modes:

  
  • "r" : Opens a text file for reading. The file must exist before it can be opened.
  •   
  • r : Opens a text file for recording. If the file does not exist, it will be created. If it already exists, the previous content will be destroyed.
  •   
  • w : Opens a text file for recording. The data will be added at the end of the file (append), if it already exists, or a new file will be created in case of the file that did not exist previously.
  •   
  • a : Opens a binary file for reading. Same as rb previous mode, only the file is binary.
  •   
  • r : Creates a binary file for writing, as in wb previous mode, only the file is binary.
  •   
  • w : Adds binary data at the end of the file, as in ab mode, except that the file is binary.
  •   
  • a : Opens a text file for reading and writing. The file must exist and be able to be modified.
  •   
  • r+ : Creates a text file for reading and writing. If the file exists, the previous contents will be destroyed. If it does not exist, it will be created.
  •   
  • w+ : Opens a text file for recording and reading. The 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 exist previously.
  •   
  • a+ : Opens a binary file for reading and writing. Same as r+b above, only the file is binary.
  •   
  • r+ : Creates a binary file for reading and writing. Same as w+b above, only the file is binary.
  •   
  • w+ : Adds data or creates a binary file for reading and writing. Same as a+b above, only the file is binary.
  •   

Additionally, it is possible that your a+ file may contain something wrong that causes occluded_triangles.txt to fail.

    
03.02.2015 / 23:42
1

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

First choice: Taking, for example, that your txt file (here named "my_file.txt") is:

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 output:

1
2
3
4
5
6
7
8
9

Remove this business from system("pause") , if the terminal is closing use getchar() for portability reasons. Always close the file with fclose and look closely at the fopen options. Also, do not fill your variable program with no motives ( v1x ) or #include s unnecessary. Finally, it would also be wise to place a conditional test if the pointer to the file returns NULL, and then only perform the intended actions based on that test.

Second option: For best results and having fewer lines of code, I recommend 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 prevents the use of fscanf , fopen and fclose . It would be ideal for your case.

    
04.02.2015 / 05:03
1

As you yourself discovered in the comments to one of the answers, the problem is the "\" inside the 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 escape characters in C strings.

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

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

    
05.02.2015 / 20:45