Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in integers only from a file.

Status
Not open for further replies.

UNH1995

Technical User
Nov 20, 2001
5
0
0
US
I have a file that I need to read in line coordinates from inorder to find the vectors and cross products. I always struggle with inputting from a file so I need some help.

The file is in this format:

L1= (35,70) (35,4) L2 =(63,4) (75,91)
L1= (65,91) (16,26) L2 =(92,61) (93,89)
L1= (77,3) (50,35) L2 =(39,5) (27,32)
L1= (10,30) (68,47) L2 =(51,23) (30,82)
L1= (9,95) (64,64) L2 =(17,52) (27,91)
L1= (3,11) (64,84) L2 =(2,24) (54,43)
L1= (56,45) (41,2) L2 =(68,65) (84,48)
L1= (20,75) (54,99) L2 =(2,20) (90,84)

The numbers represent the endpoints of each lines (2 lines). I need to extract only the numbers, prefferably in order and perform some calculations on them. Can anyone tell me how to evaluate a line at a time or pull out only the numbers?
 
If you know the format of your file is always going to be like the one you gave above you can just read in 5 char's at the beginning 'L1= (' and then read a integer '35' then a char ',' and so on.
 
L1= (35,70) (35,4) L2 =(63,4) (75,91)

I would read in an entire line as a string and then
search the string for a ( character, copy the next set of
characters to a buffer (i.e. a character string) until a ) is encountered. Continue searching for another ( and again place the characters into a second buffer until ) is encountered. Repeat procedure for third and fourth buffers.

The buffers will then contain

Buffer1 35,70
Buffer2 35,4
Buffer3 63,4
Buffer4 75,91

You can simply do a sscanf on the four buffers to get the
required numbers (i.e. sscanf (Buffer1, "%d,%d", &L1, &L2))
 
you get a line from the file to char array chmessage, and then try this,

int x[10];
sscanf(chmessage,"L%d= (%d,%d) (%d,%d) L%d =(%d,%d) (%d,%d)",
&x[0], //
&x[1],&x[2],&x[3],&x[4], // (35,70)(35,4)
&x[5], //
&x[5],&x[6],&x[7],&x[8]); // (63,4)(75,91)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top