Huge array

This is a discussion on Huge array within the C++ Programming forums, part of the General Programming Boards category; I am trying to figure out a way to read in coordinates from a text file. I know the basic ...

  1. #1
    Shadow12345
    Guest

    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.
    Last edited by Shadow12345; 09-29-2002 at 11:58 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    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
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Shadow12345
    Guest
    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!

  4. #4
    Shadow12345
    Guest
    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;
    }

  5. #5
    Shadow12345
    Guest
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Using a huge 3D array
    By Gabu in forum C++ Programming
    Replies: 3
    Last Post: 07-20-2002, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21