Reading from a file to an array ..

Hi I am trying to use a code that reads coordinates from a text file in an array and passes three coordinates to a function in opengl. Well opengl is not my topic so I will post the code for getting and passing the values ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void renderScene(void) 
{
  FILE *file;
  float numbers[3]; 
  float rx,ry,rz;
  int i;
  int b;
  file = fopen("C:\\Users\\RoBa\\Desktop\\test.txt", "r");

  while(!feof(file))
        {   b = 0;
		
            fscanf(file, "%f %f %f ", &numbers[b],&numbers[b+1],&numbers[b+2]);
			
	    rx = numbers[b];
	    ry = numbers[b+1];
	    rz = numbers[b+2];
	    cout<<rx<<endl;
	    cout<<ry<<endl;
	    cout<<rz<<endl;		
	    glLoadIdentity();
	    gluLookAt(rx , ry , rz , 0.0f,0.0f,0.f,0.0f,1.0f,0.0f);
			
}


then I call this function in main function. Now my text file is like
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
0 0 0

"I took these numbers only for testing purpose !"

Now what I get as output is an infinite loop i.e. numbers are writen from 1st line to last line then repeated again and again.

Ultimately I want to pass these coodinates to gluLookAt() function
But it takes only last value !!

By the way , gluLookAt() is function to set position of camera (I am giving this info just for sake of understanding what I am trying to do all this for !)


Can anybody tell me what is wrong with the code?
Thank you .
Rohit
I tried your code and I did not get an infinite loop. I took out:
1
2
glLoadIdentity();
gluLookAt(rx , ry , rz , 0.0f,0.0f,0.f,0.0f,1.0f,0.0f);

to center on the c++ code.

cheers
Are you calling the renderScene() function lots of times? Or does your GL library call it every time it wants to render a scene?

Also, its odd to see a mixture of old C style io code and C++.

You could use this instead of your fscanf()

1
2
3
4
5
6
7
8
9
10
11
12
13
std::ifstream file("C:\\Users\\RoBa\\Desktop\\test.txt");

// ...

while(ifs >> rx >> ry >> rz)  // only loop if read successful
{
    numbers[0] = rx;
    numbers[1] = ry;
    numbers[2] = rz;
    // ...

}


EDITED to fix blatant error.
Last edited on
std::ifstream file("C:\\Users\\RoBa\\Desktop\\test.txt", "r");
"r"
Um...
LOL! - okay, I'll go fix it ;o)
std::ifstream file("C:\\Users\\RoBa\\Desktop\\test.txt", "r");
"r"

Um...


er...

ifstream::in

;-)
Thank you guys !
I will try to implement what Galik has suggested .. and come back

Cheers
Rohit
well here's a try, as i was bored.
note absolutely no error checking, not production ready.
put filename on commandline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
#include <cstdio>


using namespace std;

// use deque list or vector
template <typename T>
void slurp(T & v, std::istream & in)
{
    int coord[3];
    while( in >> coord[0] >> coord[1] >> coord[2]) {

        v.push_back(vector<int>(coord, coord + 3));
    }
}

void dump(vector<int>& arg)
{
    printf("%d:%d:%d\n", arg[0], arg[1], arg[2]);
}

int main(int argc, char ** argv) {

    if(argc != 2) std::exit(0);
    std::ifstream in(argv[1]);
    vector< vector<int> > v;
    slurp(v, in);
    for_each(v.begin(), v.end(), dump);
}

quick too!
a file of 1,500,00 lines, 24Mb

1
2
 $ time ./slurp 11 > 1                                           
    2.16s real     1.85s user     0.29s system


1
2
$ time perl -ne '@L = split;print "$L[0]:$L[1]:$L[2]\n" ' 11 > 1
    3.96s real     3.88s user     0.07s system



Last edited on
Hi Galik .. your trick worked fine !!
Now I tried using Sleep() for the delay but it doesn't give proper delayed rendering .. I am using loops now to cause delays !! Is there any other better way ??
Thank you very much for help guys !!

Rohit
Topic archived. No new replies allowed.