|
21 Nov 2002, 14:28
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
C/C++: Algorithm problem (reading from a file)
I want to read an x number of coordinates from a file with the following syntax:
f 1/2 3/4 5/6
I planned to read it out the following way:
Code:
FILEHANDLE: fp
DATA-ARRAY: pts[10][2]
for(i=0;i<10;i++)
{
fscanf(fp,"%d/%d", &pts[i][0], &pts[i][1]);
//WHEN DO I BREAK?!
}
I don't know how long this list of coordinates is. What I can't figure out when to break from the loop. As far as I know fscanf keeps reading untill the loop is done, thus reading in more coordinates than I want it to.... no?
Any suggestions are appreciated.
__________________
"Yay"
|
|
|
21 Nov 2002, 14:37
|
#2
|
Banned
Join Date: Jul 2001
Posts: 98
|
Quote:
The value EOF
is returned if an input failure occurs before any conversion such as an end-of-file occurs. If an error or end-of-file
occurs after conversion has begun, the number of conversions which were successfully completed is returned
|
maybe that will help, just check for EOF?
|
|
|
21 Nov 2002, 15:11
|
#3
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Not quite
EOF only indicates the end of the file
I tried the following code now:
Code:
if((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))!=1) break;
This doesn't pass either, "code" got a value of 2 (got no idea what 2 exactly means *looks throught his book*).
[edit]
I'm trying something out now....
I might have been a dumbass and fscanf might return the number of successfull conversions instead of simply a 1
[/edit]
[edit2]
OK, it works now.
For those interrested how to do something like this, here's the code:
Code:
void Object3D::readFace()
{
Plane p;
int pts[10][2];
int i,code;
for(i=0;i<10;i++)
{
if((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))!=2)
{
break;
}
}
p = Plane(i,pts);
poly.push_back(p);
}
[/edit2]
__________________
"Yay"
Last edited by Structural Integrity; 21 Nov 2002 at 15:38.
|
|
|
21 Nov 2002, 15:21
|
#4
|
Banned
Join Date: Jul 2001
Posts: 98
|
sorry, i didnt paste the whole lot
Quote:
RETURN VALUE
These functions return the number of input items assigned, which can be fewer than provided for, or even zero, in the
event of a matching failure. Zero indicates that, while there was input available, no conversions were assigned; typi*
cally this is due to an invalid input character, such as an alphabetic character for a `%d' conversion. The value EOF
is returned if an input failure occurs before any conversion such as an end-of-file occurs. If an error or end-of-file
occurs after conversion has begun, the number of conversions which were successfully completed is returned.
|
sorry
|
|
|
21 Nov 2002, 15:40
|
#5
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by Pineapple
sorry, i didnt paste the whole lot
sorry
|
Don't worry, I still love you tho
And it works now!
\ /
__________________
"Yay"
|
|
|
21 Nov 2002, 15:56
|
#6
|
Banned
Join Date: Jul 2001
Posts: 98
|
Re: C/C++: Algorithm problem (reading from a file)
Quote:
Originally posted by Structural Integrity
I don't know how long this list of coordinates is.
|
why not keep looping till it doesnt return 2, instead of the for loop you suggested?
|
|
|
21 Nov 2002, 16:04
|
#7
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Possibly more elegant...
Code:
void Object3D::readFace()
{
Plane p;
int pts[10][2];
int i=0,code;
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
{
p = Plane(i,pts);
poly.push_back(p);
i++;
}
}
I always try to avoid using breaks to get out of loops but that's possibly just my coding style
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 16:07
|
#8
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Quote:
Originally posted by Gayle28uk
Possibly more elegant...
Code:
void Object3D::readFace()
{
Plane p;
int pts[10][2];
int i=0,code;
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
{
p = Plane(i,pts);
poly.push_back(p);
i++;
}
}
I always try to avoid using breaks to get out of loops but that's possibly just my coding style
|
You should also try not to index out of array elements too ...
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
21 Nov 2002, 16:19
|
#9
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Quote:
Originally posted by MT
You should also try not to index out of array elements too ...
|
I was only fixing the loop:P
I'd have either used a linked list or an array based on the file size instead if I'd been writing it from scratch
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 16:26
|
#10
|
Banned
Join Date: Jul 2001
Posts: 98
|
Code:
void Object3D::readFace()
{
Plane p;
int pts[10][2];
int i=0,code;
// the while loop is only used to fill pts?
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
;
// pts is filled
p = Plane(i,pts);
poly.push_back(p);
}
|
|
|
21 Nov 2002, 16:27
|
#11
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Gayle, your solution wouldn't work because it would add an element to the vector every time the fscanf is called. I want to make a Plane object with all the points collected in the loop instead.
__________________
"Yay"
|
|
|
21 Nov 2002, 16:39
|
#12
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Pineapple, you've forgotten to postcrement i ;p
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
21 Nov 2002, 16:39
|
#13
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
???
As far as I can see it does exactly the same as yours, it just uses while instead of for and if and it will go on for as long as there is data in the file (although without array bounds checking).
Although it has been a while (no pun intended) since I did anything in C so I could be missing something.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 16:45
|
#14
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Quote:
Originally posted by MT
Pineapple, you've forgotten to postcrement i ;p
|
Is it just me or can he^^^ spot a coding error from 50 miles away?
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 16:45
|
#15
|
Banned
Join Date: Jul 2001
Posts: 98
|
Yours was calling plane() for every iteration, when I think the idea was that plane was called when pts was filled.
|
|
|
21 Nov 2002, 16:48
|
#16
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
So does mine. If code!=2 (i.e. if pts is not filled) the loop ends and it doesn't iterate.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 16:59
|
#17
|
Banned
Join Date: Jul 2001
Posts: 98
|
Quote:
Originally posted by Gayle28uk
Possibly more elegant...
Code:
...
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
{
p = Plane(i,pts);
...
I always try to avoid using breaks to get out of loops but that's possibly just my coding style
|
Plane(i,pts) is supposed to be used only when all the data has been read, not for each set of data.
|
|
|
21 Nov 2002, 17:07
|
#18
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by Pineapple
Plane(i,pts) is supposed to be used only when all the data has been read, not for each set of data.
|
indeed....
It wasn't that obvious tho, but it's the constructor of a Plane object (this is fully C++ \o/). After that you put the newly made Plane object in the vector with push_back(p).
__________________
"Yay"
|
|
|
21 Nov 2002, 17:17
|
#19
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
With you now
Change
Code:
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
{
p = Plane(i,pts);
poly.push_back(p);
i++;
}
to
Code:
while((code=fscanf(fp, "%d/%d",&pts[i][0],&pts[i][1]))==2)
i++;
p = Plane(i,pts);
poly.push_back(p);
and it works as intended.
Although the way I'd have implemented it (I can't actually code it cos I really can't remember enough to get it exact) would be to use int *pts and pts=calloc(number_of_file_elements, sizeof(int)) to create the array dynamically. And no, I can't remember how to get the filesize (int filelength(fp)?)
If I remember right that could still be addressed using pts[i][0] but that's one part I'm not sure of.
Damn I should leave this sort of stuff alone, it's just far far too long ago
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
21 Nov 2002, 17:30
|
#20
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
I didn't give you enough information to know that that wouldn't work GayLe. (you own solution with the filesize)
The code is intended to read an object file, which is basically a list of points and planes that are linked together to create an object.
Each point or plane is put on a seperate line:
v 1 1 1 //create vector point (1,1,1 in xyz)
v -1 1 1
v -1 -1 1
v 1 -1 1
v 1 1 -1
v -1 1 -1
v -1 -1 -1
v 1 -1 -1
vt 0 0 //create 2D vector point for texture mapping (x,y)
vt 1 0
vt 1 1
vt 0 1
f 2/0 3/1 1/2 0/3 //(link points together to a field (v2/vt0 v3/vt1 v1/vt2 v0/vt3)
f 6/0 7/0 4/0 5/0
The texture mapping and texture coordinates are needed to map a texture on a plane correctly.
The list above would create two planes opposite eachother.
__________________
"Yay"
|
|
|
21 Nov 2002, 17:49
|
#21
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
I'll stop trying to be clever now hehe
I never did understand 3D graphics very well Could do you a lovely copper bar equivalent using a Tseng Labs ET4000 though
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
|
All times are GMT +1. The time now is 16:44.
| |