• Advertisement
Sign in to follow this  

file scanning / reading issue - SOLVED!

This topic is 4607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I have a small problem that i wonder if you could help out with. I'm not a student, so its not homework! I have a file which has vertx data like so: 12 34 56 23 45 76 12 56 78 12 3 45 ................. every three lines constitutes the vertices of a triangular surface I would like to be able to read in 3 lines to an array of some kind (I figure a 3x4 matrix for [vertex id][x][y][z] * 3 ) process as required, write values to file and then read in the next three lines I use a tokenize function to read in each line of data, to cut up the string via whitespaces so that I can get access to each coordinate number. Tokenize returns a vector<string> 'array' if you like. I can convert the vector string array component required to a c string using c_str() and convert that c string using strtod( xxx ,0) so that I can assign the component to a double array. I just cant figure out a way to read in 3 lines to an array to get the coordinates of the triangle corners and then move on to reading in the next three lines. my current code looks like this: #include <iostream> #include <fstream> #include <stdlib.h> #include <string> #include <sstream> #include <stdio.h> #include <ios> #include <vector> #include <algorithm> #include "interfile.h" #include "tokenize.h" #include "finalpath.h" #include "trilen.h" #pragma warning (disable : 4786) using std::string; using std::iostream; using namespace std; int main() { //1.first define variables //variables mean:- stlinp = stl file path, intfpath = intermediate file path, str = string for first getline //vstr = string for second getline, a = counter in while loop (all ints are counter variables), vector <string> = vector of string type to test if token[0] = vertex //str = stl file line, fopath = final output path for model data, string stlinp="", intfpath="", fopath="", finaloutpath="", vstr="", str, dum, stlinputpath=""; int a=0, b=0, c=0, d=0, e=0; vector<string> iftest(1); iftest[0] = "vertex"; //2.then ask for an input path cout << "Enter path of .stl file below:" << endl; cin >> stlinputpath; cout << "DEBUG1:input path you entered was: " << stlinputpath << endl; //3.pass input path for processing in interfile function to create interfpath, same for final path interfile(stlinputpath, intfpath); cout<< "DEBUG2: intermediate file path is: " << intfpath << endl; finalpath(stlinputpath, finaloutpath); cout << "DEBUG3: final output path is: " << finaloutpath << endl; //*****function writes intermediate file************************************* ifstream rin(stlinputpath.c_str()); ofstream wrout(intfpath.c_str()); while(!getline(rin,str).eof()){ vector<string> tokens; Tokenize(str, tokens); //pass str to Tokenize function in tokenize.h //-its quite easy to play with each component of each line, but how do I read and apply multiple lines???? } wrout.close(); rin.close(); return 0; } so any clues on this would be good, I wouldnt have thought that somthing like: while(!eof()){ for(i=0; i=2; i++){ //-readin line //-tokenize line //-set array at equal to xyz components } //-do array processing } would work...it would just keep reading in the first three lines?? I should explain, I am an aerospace engineer and am attempting to learn to write a flight simulator which combines the best parts of two commercial sims, to get the 'best' possible sim. this is just the start of a long and no doubt convoluted project. My programming experience extends to: Uni module on C, self taught VB6 and self taught c++ (an ongoing process! ). I figure, 1. get a model file I am happy with, 2.display that model file and then move on to other stuff after that. ANY help will be greatly appreciated! thanks coanda [Edited by - coanda on July 12, 2005 3:13:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
You need to set the tokenizer to a blank space.

while(getline(ifstream,string,' ')){

}

Also, if you have multiple spaces you can ignore them within the loop until you get a value

Share this post


Link to post
Share on other sites
gink,

i can get my data such that each line is tokenized using the white spaces, so that:

tokens[0] = first value on first line
tokens[1] = second value on first line
tokens[2] = third value on first line

i can then put those values into the first row of an array

my issue is that I would like to read in three lines and put them into the array, at a time, process the array and then read in the next three lines, so, one line at a time is fine......but three lines at a time is proving to be a problem.

Share this post


Link to post
Share on other sites
I've solved the issue, i was initialising my for-loop incorrectly, so it was always at a count of 3.

ie:

for(i=0; i=3; i++)

which i now realise will always return i as 3, and not count UP!

so I now use:

for(i=0; i<3; i++)

thought 'exposing' this silly error might help someone else!

coanda

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement