How to read another file again?

Hello, guys.

I have no idea how to get information from another files.

Right now, what I’m doing now is a kind of a monitoring system like seismogram. This program reads a input file and then display it on screen. It could read the file, but it did just once. So, I changed the position of the function. Then, it could not display on screen.

My program consists of some functions such as main, input, display, timer and so on.

I do not have any idea the position input should be located.

When input was in main, displaying is fine but I cannot read data again. It was displaying the same data on screen.

When input was in display, displaying is fine but it was like a picture. I mean, it displayed data at once, but it is not I want to do.

When input was in timer, I could not see anyting on screen.

I really need your help. T^T

You really need a book about C++.

Hello, I know how to read and write a file.

I asked how to make a program for displaying data well but not reading files.

Hello :slight_smile:

bigarray = readfile();

displaydata(bigarray);

What part you fail to implement ?

Looks like you jumped into too deep water for you current knowledge/experience.
My guess is that you took some tutorial code and started modifying it without understanding how it works.
So my advice is to go back one step in education and fill the gap that you left behind - read that sample code from top to bottom and make sure you understand what every function does and when is it called.

I didn’t have full understanding of sample codes I used in the beginning of my C++/OpenGL programming but I allready had strong skills from other programming languages inluding hardcore assembler programming, so I got away with skipping some lessons.

Thanks for your advice. I will go back one step.

Hello,

Reading data is pretty fine. I used big array for a input file and was successful to display data at once or every second as real time.

Here is my problem. Even though my program read input data continuously whenever it is called, it does not display any data on screen. I thought I did not have enough background of opengl. I was just wondering how to fix my code to work well…

Anyhow, thank you for your reply.

What is your display loop structure then ? What windowing system do you use ? Glut ? Win32 api ? X ?

I’m using GLUT on windows. I’m also doing on Mac OSX. (xcode)

Then, the loop structure is as following:

display()
{
init_display;;

input(“filename”);
draw(); // this is for real-time displaying
}

or

main()
{

input(“filename”);

}

display()
{
draw(); // this is for real-time displaying
}

The first one can read file every time it is called, but it cannot draw data on display even though draw is called. I do not have any idea. I was supposed that I should have changed the glutTimerFunc’s variables. But, it was not a solution.

The second one can read file once, of course. So, I modified the code like the first one.

I think there is time gap to display data. I tried to read other file using a flag array. The initial value was setting to read input data, but it did not. The structure was that if the data was read, it had to set the flag such as reading and store ‘0’ value to buffer(it was displayed and set to ‘0’). So, I expected it displayed the data and then ‘0’ value on screen. But, it showed just ‘0’ value on screen. I think I missed something important.

Do you have any idea about it?

Anyway, thank you for your concern.

The first one can read file every time it is called, but it cannot draw data on display even though draw is called. I do not have any idea.

Well that is a problem. It should work.
Can you give more details about your code ?

Thanks for your time. Here is my simple code.


// is it new file?
int new_file = 1;
int i = 0;

// Read input data from a file
void input(char * filename)
{
read input data and store into buffer(global array).
then set i to 0
}

// Initialization of OpenGL
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
gluOrtho2D((GLdouble)screenwidth*(-1.0), (GLdouble)screenwidth, (GLdouble)screenheight*(-1.0), (GLdouble)screenheight);
}

void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(1, Timer, 0);
}

void resize(int w, int h)
{
//resize windows
}

// To Display data on screen
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0f, 0.5f, 0.0f);
glBegin(GL_LINE_LOOP);
	glVertex2i(-500, -250);
	glVertex2i(-500, 250);
	glVertex2i(500, 250);
	glVertex2i(500, -250);
glEnd();
...

//input("s_wave.txt");
draw(i%1000);
i++;
glutSwapBuffers();

}

// Original One

void draw(int index)
{
glBegin(GL_LINES);
for(int t = 0; t < index; t++)
{
if(new_file == 1)
{
glVertex2f(x[t], y[t]);
glVertex2f(x[t+1], y[t+1]);
}
else
{
glVertex2f(x[t], y_temp[t]);
glVertex2f(x[t+1], y_temp[t+1]);
}
}
glEnd();

//new_file = 0;

}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screenwidth, screenheight);
glutInitWindowPosition(50, 50);
glutCreateWindow(“SEISMOGRAM PROTOTYPE”);
glutKeyboardFunc(keyboard);
init();

    input("s_wave.txt");

    glutDisplayFunc(myDisplay);
glutReshapeFunc(resize);
glutTimerFunc(0,Timer,0);
glutMainLoop();

}

If input function is in main, it can display data every second but there is no update. Also, it has a flag, new_file. It indicate the data is new or not. If it is old one, the code sets new_file to 0 and the values of buffer must set to 0. I guess the code displays input data and then display 0 on screen. But, I can see just ‘0’ every second.

If input is in display, I could not see any data on screen.

I know my simple code is quite bad but if you give me advice to fix, I will be really happy.

I really appreciate your time. Thanks.

glutTimerFunc argument is in milliseconds, so put 1000 to have 1 second intervals.

I don’t understand this part :
draw(i%1000);
i++;
used to limit the loop here :
for(int t = 0; t < index; t++)

Is that the number of lines you want to read from the file ? In this case, the ‘index’ value should be set inside your input() function.

My guess is that everything (apart the timer value) is ok on the GL and Glut sides.

Try to do tests with a very simple input file (5 lines max, with recognizable values), and use printf a lot to see values and trace execution paths. Remove your newfile/oldfile stuff, apparently you are doing it all wrong, just consider it is always a new file. Use also a debugger if you can (sometimes not so easy with real time apps).

Thank you for your advice.

The reason I used draw(i%1000) was that i was a global variable and the line of input file was 1000. That is, ‘i’ should have ‘0’ again. Also, unless I used the global variable, i, my code did not show data as real-time histogram on screen. So, I should do like that.

Then, I will try to do with simeple code. I really appreciate your help. I was wondering whether I can ask something to you if I have another question.

Have a good day.