PDA

View Full Version : Newbie i/o datafiles HELP PLEASE!



09-30-2001, 05:27 PM
Hey everyone. I haven't taken C for about 7 years now and I can't find any of my books on it.

First of all, I have a data file full of vertices that I need to draw on the screen.
It looks like this
"a1.dat"
0.4606 0.48759
0.47104 0.49414

0.47104 0.49414
0.47108 0.49871

0.47108 0.49871
0.46883 0.50184

0.46883 0.50184
0.464 0.50854

0.464 0.50854
0.45744 0.51764

0.45744 0.51764
0.44479 0.50948

0.4606 0.48759
0.44479 0.50948

0.44479 0.50948
0.45744 0.51764

0.45744 0.51764
0.45454 0.54415

0.45454 0.54415
0.44198 0.53584
and so on and so on

I need it to read from a file instead of manually inserting them myself, since there are thousands of them: IE I DON"T WANT TO DO THIS http://www.opengl.org/discussion_boards/ubb/wink.gif
glBegin(GL_LINES);
glVertex3f(0.4606,0.48759,0.0);
glVertex3f(0.47104,0.49414,0.0);
glVertex3f(0.47104,0.49414,0.0);
glVertex3f(0.47108,0.49871,0.0);

glVertex3f(0.47108,0.49871,0.0);
glVertex3f(0.46883,0.50184 ,0.0);

glVertex3f(0.46883,0.50184,0.0);
glVertex3f(0.464,0.50854 ,0.0);


I said again that I don't have any C source books around so I need code! You guys love to do this stuff and I'm in big trouble!

Tolga Dalman
09-30-2001, 06:12 PM
hi!

hm, i'm quite new to c aswell, but try the following code:
//
char s[N*MAX_STR_LEN];
float your_float;
int i=0;

f=fopen(filname,"r");

while((s[i]=gets(f)!=EOF){
your_float=atof(s[i]);
i++;
}
//
of course, there's better code, but spontaneously i can't remember of a better one. also, it's very late (rather early) here...

good luck,
tolga.

ps: try www.about.com. (http://www.about.com.) there's a number of good c-tutorials ;-)

09-30-2001, 08:31 PM
bump

Gavin
10-01-2001, 12:37 AM
float x, y;

fscanf(file_in, "%f %f", &x, &y);

glVertex3f(x, y, 0);

neatest way to do it would be to get the file size by using fseek. Then malloc an arry of that size * float, then you can read all your data in the memory. And can access it as necessary. Or if speed is a necessity use low level IO i.e. fread instead of fscanf.