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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading integers from a file.

Status
Not open for further replies.

UNH1995

Technical User
Nov 20, 2001
5
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?
 
Try this. Not saying it's perfect but it'll help you get started thinking about it. Havn't tested though so may have bugs. Hope not. Look correct

Personally when designing a file format I would know how big each piece of data is in the file and pad it in with spaces where it is too short then I would move the file pointer to the place I wanted to start reading from. Don't forget it is a data file it doesn't have to be laid out tidy on each line. You can write it in 1 big linear line and move the file pointer. It is much quicker this way.

Any way I've made a quick function to read the numbers.

int ExtractBraces(FILE *pFile)
{
char pBuffer[4];
int iReadNumber;
int ch, iBraceCheckLoop = 0, iNumReadLoop = 0;


for (;;) {
ch = fgetc(pFile);
pBuffer[iBraceCheckLoop] = (char)ch;
iBraceCheckLoop++;

// If an open brace is read, read the next character into the buffer
if (ch == '(') {
// Empty Buffer
strcpy(pBuffer, "");
for(;;) {
// Read next character or first number
ch = fgetc(pFile);
pBuffer[iNumReadLoop] = (char)ch;
iNumReadLoop++;
// Check it's not the closing brace
if (ch == ')') {
// If a closing brace was found terminate the string in the buffer
// being sure to write over the closing brace in the buffer
pBuffer[iNumReadLoop - 1] = '\0';
// Convert buffer string to integer and return
iReadNumber = atoi(pBuffer);
return iReadNumber;
}
}
}
}
}

Now when calling the function do this.

// Open the file
pFile = fopen("filename.xxx", "r");
// Call ExtractBraces as many times as needs be
// In a loop might be usefule
for (i = 0; i < numberoftimes; i++) {
int iReturnedNumber = ExtractBraces(pFile);
// Put the returned number somewhere
int iGlobalNum1 = iReturnedNumber; // etc
}
//close the file when finished reading all numbers
fclose(pFile);

NOTE: While a file is open the file pointer will always point to the next character after the one read which is why you should read all data before closing the file. If you close the file and re-open you will have to move the pointer to the correct place before reading again.

Hope this all helps.
 
Oops forget to check for the comma between the two numbers within the braces but I'm sure you can work that bit out by adding extra character checks.
HINT when checking for the closing brace check for both the comma and the closing brace and when checking for an opening brace check for both an opening brace and a comma. This should fix that problem.

See Ya.
Ultimatemind
 
You can use the following look-like stupid way to get what you want.
The result is in (x[1],x[2])(x[3],x[4]) and (x[6],x[7])(x[8],x[9])
void main(void)
{
ifstream fn;
char chmessage[1024];

fn.open(&quot;test.txt&quot;, ios::in);
int x[10];char a[5];
do{
fn.getline(chmessage,sizeof(chmessage),'\n');
sscanf(chmessage,&quot;%c%d%3c%d%c%d%3c%d%c%d%3c%d%3c%d%c%d%3c%d%c%d&quot;,
&a,&x[0],&a, //
&x[1],&a,&x[2],&a,&x[3],&a,&x[4], // (35,70)(35,4)
&a,,&x[5],&a, //
&x[6],&a,&x[7],&a,&x[8],&a,&x[9]); // (63,4)(75,91)

}while (!fn.eof());

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top