[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

File loader.

Hey everyone, I wrote a small program that loads a file with some decimal numbers in it like 2.333,433.222,343 etc... Then stores the file in a vector and prints it. It works when I try just one file but when I try to load to it crashs. Please help.
#include <vector>
#include <string>
#include <fstream>
#include <sstream> // for stringstream
#include <iterator> // for ostream_iterator
#include <algorithm> // for for_each
#include <iostream> // for cout
#include <limits> // for int max
using namespace std;
typedef vector<float> v; // the numbers from the current line
vector<float> v2;
typedef vector< v > theVectors; // vectors from all lines
typedef vector<int> numofthings;
theVectors::iterator myIter;    
numofthings num(10000);
v test(10000);
theVectors hold(10000);
int i=0;
int i2=-1;
float x;
struct matrix { float x1;float y1;float z1;float w1;
                        float x2;float y2;float z2;float w2;
                        float x3;float y3;float z3;float w3;
                        float x4;float y4;float z4;float w4;}mat;
matrix nulls={1,0,0,0,
                    0,1,0,0,
                    0,0,1,0,
                    0,0,0,1};

void openraw(char raw[])
{
fstream in(raw);
string line;
i2++;
const int l = numeric_limits<int>::max();
while (getline(in, line)) {
for (stringstream ss(line); ss >> x; ss.ignore(l, ','))
{
test[0]=x;
hold[i]=test;
num[i2]=i;
i++;
}
num[i2]+=1;
}
}
int numofploc(int loc)
{
int ret=0;
for(int q=-1;q++;q<loc)
{
ret+=num[q];
}
return ret;
}
void renderfile(int loc)
{
int start=numofploc(loc);
cout<<start;
start-=num[loc];
int f0=0;
int f1=0;
int f2=0;
float holdv1;
float holdv2;
float holdv3;
float holdv4;
for(myIter = hold.begin()+start; myIter != hold.begin()+start+18; myIter++)
{
float x,y,z;
z=0;
float abc;
v2= *myIter;
abc=v2[0];
f0++;
if(f0==1)
{
x=abc;
}
if(f0==2)
{
y=abc;
}
if(f0==3)
{
z=abc;
f0=0;
}
f1++;
if(f1==3)
{
f1=0;
f2++;
if(f2==1)
{       
holdv1=nulls.x1;
holdv2=nulls.y1;
holdv3=nulls.z1;
holdv4=nulls.w1;
x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x2;
holdv2=nulls.y2;
holdv3=nulls.z2;
holdv4=nulls.w2;
y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x3;
holdv2=nulls.y3;
holdv3=nulls.z3;
holdv4=nulls.w3;
z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
cout<<x<<" "<<y<<" "<<z<<" "<<endl;
}
if(f2==2)
{
holdv1=nulls.x1;
holdv2=nulls.y1;
holdv3=nulls.z1;
holdv4=nulls.w1;
x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x2;
holdv2=nulls.y2;
holdv3=nulls.z2;
holdv4=nulls.w2;
y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x3;
holdv2=nulls.y3;
holdv3=nulls.z3;
holdv4=nulls.w3;
z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
cout<<x<<" "<<y<<" "<<z<<" "<<endl;
}
if(f2==3)
{
holdv1=nulls.x1;
holdv2=nulls.y1;
holdv3=nulls.z1;
holdv4=nulls.w1;
x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x2;
holdv2=nulls.y2;
holdv3=nulls.z2;
holdv4=nulls.w2;
y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
holdv1=nulls.x3;
holdv2=nulls.y3;
holdv3=nulls.z3;
holdv4=nulls.w3;
z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);
cout<<x<<" "<<y<<" "<<z<<" "<<endl;
f2=0;
}
}
}      
myIter=NULL;
}
void main()
{
openraw("test.RAW");
openraw("test2.RAW");
int pass=numofploc(0);
renderfile(0);
renderfile(1);
int a;
cin>>a;
}
Here is the code. For completness sake I am also including the files I am loading:
50,0,0
0,0,0
250,50,0
50,0,0
0,0,0
25,50,0
file2:
250,20,0
0,30,0
2530,250,0
50,0,0
0,0,0
25,50.3,0
0
laeuchli
Asked:
laeuchli
1 Solution
 
chensuCommented:
Can't you debug it?
0
 
nietodCommented:
>> It works when I try just one file but
>> when I try to load to it crashs
Huh?   Does it work or does it crash?

Where is it crashing?
0
 
laeuchliAuthor Commented:
sorry, I meant that is crashs when I load two files.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
Where does it crash?

Your openraw() function is using global variables that it does not initialize, like i2 and num[].  These variables are going to have different values the 2nd time openraw is run.  Tha may be a problem.  Like if the two files are so large that together they overrun num[] you will get a crash
0
 
laeuchliAuthor Commented:
it crashs after renderfile is called the second time, and it crashs at     v2= *myIter;
0
 
nietodCommented:
What is start at that time?  You alter start in

int start=numofploc(loc);
cout<<start;
start-=num[loc];

then use start in the for() loop.  If start is set wrong that could cayuse a problem.  We can't tell that because we don't know anything about the files you are reading.
0
 
sludinCommented:

I believe you problem is centered around these line:

    int start=numofploc(loc);    
    start-=num[loc];

and this function:

    int numofploc(int loc)
    {
        int ret=0;
        for(int q=-1;q++;q<loc)
        {
            ret+=num[q];
        }
        return ret;
    }

Check out the for loop.  No matter what value of loc is passed in you always return num[0] ( == 19 ).

On pass one of the lines from renderfile() you end up with

start == 0  ( 19 - 19 )

Which is perfect.

After pass two you end up with

start == -17  ( 19 - 38 )

Which will eventually bomb when you access the wrong thing.

If what you want to do is start at the first index for the second file in the hold vector you want to flip the subtraction:

    int start=numofploc(loc);    
    start = num[loc] - start;

This will make it work with your current logic.

Hope that helps,

-stephen





0
 
laeuchliAuthor Commented:
nietod yes you know about what files I am loading because i posted them, and sludin I will give it a try and let you know.
0
 
laeuchliAuthor Commented:
thank you. it worked
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now