User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 21 Nov 2002, 14:28   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 14:37   #2
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
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?
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 15:11   #3
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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.
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 15:21   #4
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
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
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 15:40   #5
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 15:56   #6
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
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?
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 16:04   #7
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 16:07   #8
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
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
MT is offline   Reply With Quote
Unread 21 Nov 2002, 16:19   #9
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 16:26   #10
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
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);
}
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 16:27   #11
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 16:39   #12
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Pineapple, you've forgotten to postcrement i ;p
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 21 Nov 2002, 16:39   #13
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
???

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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 16:45   #14
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 16:45   #15
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
Yours was calling plane() for every iteration, when I think the idea was that plane was called when pts was filled.
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 16:48   #16
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 16:59   #17
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
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.
Pineapple is offline   Reply With Quote
Unread 21 Nov 2002, 17:07   #18
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 17:17   #19
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Unread 21 Nov 2002, 17:30   #20
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 21 Nov 2002, 17:49   #21
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
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!!!
Gayle29uk is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 17:55.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018