-
Huge array
I am trying to figure out a way to read in coordinates from a text file. I know the basic file i/o, that isn't the problem. The problem is the fact that i have an insane number of coordinates to read from the file and I am looking for a good way to do it. There are about 872 lines, each line holds the X, Y, Z coordinates for a triangle so that means 9 floating point numbers. This will take up a lot of memory and I need the most efficient way to do it. Should I create a huge array of (872 * 9) floating point integers?
such as float* Verticies[872] and then read in the coordinates with a for loop? should I try using a vector somehow?
EDIT:
Okay I have devised my first algorithm for this problem, I just need help implementing it
1) Make sure fstream is included in the project
2) Open the file for input
3) Declare an object of ifstream
4) while not at the end of the file read each line until 6 parenthesis have been encountered, when the 6th has been encountered skip to the next line and continue reading from there
This is what a line of the text file looks like:
( 192 -1024 59 ) ( 192 -1024 -763 ) ( 192 -256 -763 ) 64CRATE0 [ 0 1 0 0 ] [ 0 0 -1 0 ] 0 1 1
after the numbers there is some extra 'code' or whatever generated by the program that produces this text file. I need it to skip over that crap so it doesn't mess up my program.
I need all the suggestions I can possibly get.
-
read and write the data to a file using binary io. Store the data in structures to make things easier...
I only counted 5 variables for each triangle, but whatever. Something like this:
typedef struct {
float a, b, c, d, e, f, g, h, i;
}, Triangle;
FILE* handle = fopen("bin.dat", "rb+");
if(handle == NULL)
abort();
int x = 0;
while(fread(tri[x], 1, sizeof(Triangle), handle)) {
x++;
}
fclose(handle);
if(x == 0)
abort();
--x; //...point to last valid index
-
Okay that sounds good, I was thinking of doing something like typedef struct. I have a few questions and comments:
First of all is this comma supposed to be here?:
}, Triangle;
Second what is the second argument ("rb+") supposed to be?:
FILE* handle = fopen("bin.dat", "rb+");
Third is this supposed to be Triangle[x] not tri[x]?:
while(fread(tri[x], 1, sizeof(Triangle), handle)) {
Fourth will this only read the number values? I don't want any parenthesis accidently read in as coordinates for my verticies. Everything past the 6th parenthesi is invalid and I can't use it, is there a way to make it skip to the next line once the 6th parenthesi is encountered? There are 9 values per line because each set of parenthesis contains 3 coordinates, and after the last parenthesis everything is invalid and not needed.
Okay well thank you a bunch!
-
This only reads the first line of the text file why won't it continue to read further?
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
using namespace std;
struct Triangle {
float a, b, c, d, e, f, g, h, i;
};
vector<Triangle*> TriangleVector;
int main(void) {
ofstream fout;
ifstream fin;
fout.open("Formatted coords.txt");
fin.open("Coordinates.txt");
if(fout.fail()) {
cout << "Could not load coordinates" << endl;
cout << "Hit a key to escape" << endl;
getch();
return 0;
}
if(fin.fail()) {
cout << "Could not load coordinates" << endl;
cout << "Hit a key to escape" << endl;
getch();
return 0;
}
int x = 0;
while(!fin.eof()) {
TriangleVector.push_back(new Triangle);
fin.ignore(2, NULL);
fin >> TriangleVector[x]->a;
fin >> TriangleVector[x]->b;
fin >> TriangleVector[x]->c;
fin.ignore(5, NULL);
fin >> TriangleVector[x]->d;
fin >> TriangleVector[x]->e;
fin >> TriangleVector[x]->f;
fin.ignore(5, NULL);
fin >> TriangleVector[x]->g;
fin >> TriangleVector[x]->h;
fin >> TriangleVector[x]->i;
fout << TriangleVector[x]->a << endl;
fout << TriangleVector[x]->b << endl;
fout << TriangleVector[x]->c << endl;
fout << TriangleVector[x]->d << endl;
fout << TriangleVector[x]->e << endl;
fout << TriangleVector[x]->f << endl;
fout << TriangleVector[x]->g << endl;
fout << TriangleVector[x]->h << endl;
fout << TriangleVector[x]->i << endl;
cout << TriangleVector[x]->a << endl;
cout << TriangleVector[x]->b << endl;
cout << TriangleVector[x]->c << endl;
cout << TriangleVector[x]->d << endl;
cout << TriangleVector[x]->e << endl;
cout << TriangleVector[x]->f << endl;
cout << TriangleVector[x]->g << endl;
cout << TriangleVector[x]->h << endl;
cout << TriangleVector[x]->i << endl;
getch();
x++;
}
getch();
fin.close();
fout.close();
cout << "Hit a key to escape" << endl;
getch();
return 0;
}
-
arg yes I know that is to stop it because otherwise it just keeps on going forever. What arguments do I pass in to fin.ignore() so it will skip the rest of the line and go to the next one?