CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2016
    Posts
    24

    error using ifstream and osstream

    When i run below program for test the basic working of my program it works fine:

    Code:
    #include "map.h"
    #include <iostream>
    #include <utility>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
        bigarray arr, arr1;
            for(size_t i=0;i<=10;i++) {
        int key = arr(i, i+1, i*2, 5, 2,1,1,1,1,1,1,i,1)++;
            arr1(i,i+1,i*2,key,2,1,1,1,1,1,1,i,1)=i+1;} 
    
        ofstream ofs(dimnam, ios::binary);
        ofs << arr; 
        ofs.close();
    
        ofstream ofs1(dimnam1, ios::binary);
        ofs1 << arr1;
        ofs1.close();
    
        ifstream ifs(dimnam, ios::binary);
        ifs >> arr;
        ifs.close();
    
        ifstream ifs1(dimnam1, ios::binary);
        ifs1 >> arr1;
        ifs1.close();
    
            for (auto m = arr1.begin(); m != arr1.end(); ++m)
           cout << m->first << " = " << m->second << endl;
        return 0;
    }
    Its output is:
    Code:
    (0, 1, 0, 0, 2, 1, 1, 1, 1, 1, 1, 0, 1) = 1
    (1, 2, 2, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1) = 2
    (2, 3, 4, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1) = 3
    (3, 4, 6, 0, 2, 1, 1, 1, 1, 1, 1, 3, 1) = 4
    (4, 5, 8, 0, 2, 1, 1, 1, 1, 1, 1, 4, 1) = 5
    (5, 6, 10, 0, 2, 1, 1, 1, 1, 1, 1, 5, 1) = 6
    (6, 7, 12, 0, 2, 1, 1, 1, 1, 1, 1, 6, 1) = 7
    (7, 8, 14, 0, 2, 1, 1, 1, 1, 1, 1, 7, 1) = 8
    (8, 9, 16, 0, 2, 1, 1, 1, 1, 1, 1, 8, 1) = 9
    (9, 10, 18, 0, 2, 1, 1, 1, 1, 1, 1, 9, 1) = 10
    (10, 11, 20, 0, 2, 1, 1, 1, 1, 1, 1, 10, 1) = 11
    Now when i try to implement same principle in my main program i'm not getting proper output, my main program is:
    Code:
    #include <ros/ros.h>
    #include <sensor_msgs/PointCloud2.h>
    #include <pcl_conversions/pcl_conversions.h>
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include "map.h"
    #include <iostream>
    #include <utility>
    #include <fstream>
    #include <string>
    void
    cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
    {
    pcl::PointCloud<pcl::PointXYZRGB> output;
    pcl::fromROSMsg(*input,output);
       // some calculations here to segment an object from 3d image
      cout<<"stat saving object model"<<endl;
      if(ros::ok()) {
         int d[14];  bigarray arr, arr1;
         for(int p0=2;p0<=100 && ros::ok();p0+=5) {
            for(int p1=0;p1<a.size() && ros::ok();p1++) {
               int d1 = sqrt(pow(a.at(p1)-a.at(p0),2)+pow(b.at(p1)-b.at(p0),2)+pow(c.at(p1)-c.at(p0),2))*1000; 
               if(d1==20) { 
                 for(int p2=0;p2<a.size() && ros::ok();p2++) { 
                    int d2 = sqrt(pow(a.at(p2)-a.at(p0),2)+pow(b.at(p2)-b.at(p0),2)+pow(c.at(p2)-c.at(p0),2))*1000;
                    int d1d = sqrt(pow(a.at(p2)-a.at(p1),2)+pow(b.at(p2)-b.at(p1),2)+pow(c.at(p2)-c.at(p1),2))*1000;
                    if(d2==20 && d1d==20) {
                      float a1 = a.at(p1)-a.at(p0); float b1 = b.at(p1)-b.at(p0); float c1 = c.at(p1)-c.at(p0);
                      float a2 = a.at(p2)-a.at(p0); float b2 = b.at(p2)-b.at(p0); float c2 = c.at(p2)-c.at(p0);
                      float a3r = b1*c2-b2*c1; float b3r = a2*c1-a1*c2; float c3r = a1*b2-a2*b1;
                      float a3, b3, c3;
                      if(c3r>0) {
                        a3 = a3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                        b3 = b3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                        c3 = -c3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                      }
                      else {
                          a3 = a3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                          b3 = b3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                          c3 = c3r/sqrt(a3r*a3r+b3r*b3r+c3r*c3r);
                      }
                      float x3 = (a.at(p0)+a.at(p1)+a.at(p2)/3)+0.02*a3;
                      float y3 = (b.at(p0)+b.at(p1)+b.at(p2)/3)+0.02*b3;
                      float z3 = (c.at(p0)+c.at(p2)+c.at(p2)/3)+0.02*c3;
                      for(int p4=0;p4<a.size() && ros::ok();p4++) {
                         int d0r = sqrt(pow(a.at(p4)-a.at(p0),2)+pow(b.at(p4)-b.at(p0),2)+pow(c.at(p4)-c.at(p0),2))*1000;
                         int d1r = sqrt(pow(a.at(p4)-a.at(p1),2)+pow(b.at(p4)-b.at(p1),2)+pow(c.at(p4)-c.at(p1),2))*1000;
                         int d2r = sqrt(pow(a.at(p4)-a.at(p2),2)+pow(b.at(p4)-b.at(p2),2)+pow(c.at(p4)-c.at(p2),2))*1000;
                         int d4r = sqrt(pow(x3-a.at(p1),2)+pow(y3-b.at(p1),2)+pow(z3-c.at(p1),2))*1000; 
                         if(d0r>0 && d1r>0 && d2r>0 && d4r>0 && d0r<=70) {  
                           d[0]=p0; d[1]=p1; d[2]=p2; d[3]=d0r; d[4]=d1r; d[5]=d2r; d[6]=d4r; d[7]=ac.at(p0); d[8]=bc.at(p0);
                           d[9]=ac.at(p1); d[10]=bc.at(p2); d[11]=ac.at(p2); d[12]=bc.at(p4); d[13]=ac.at(p4); d[14]=bc.at(p4);
    
                           int key = arr(1,1,d[3],d[4],d[5],d[6],d[7],d[8],d[9],d[10],d[11],d[12],d[13])++;
                           arr1(1,key,d[3],d[4],d[5],d[6],d[7],d[8],d[9],d[10],d[11],d[12],d[13]) = d[0];
                         } 
                       }
                     }
                   }
                }
             }
          }
    
        ofstream ofs(dimnam, ios::binary);
        ofs << arr; 
        ofs.close();
    
        ofstream ofs1(dimnam1, ios::binary);
        ofs1 << arr1;
        ofs1.close();
    
        ifstream ifs(dimnam, ios::binary);
        ifs >> arr;
        ifs.close();
    
        ifstream ifs1(dimnam1, ios::binary);
        ifs1 >> arr1;
        ifs1.close();
    
            for (auto m = arr1.begin(); m != arr1.end(); ++m)
           cout << m->first << " = " << m->second << endl;
    
       }
    }
    
    
    
    int
    main(int argc, char** argv)
    {
        ros::init(argc, argv,"godgift");
        ros::NodeHandle nh;
        ros::Subscriber sub = nh.subscribe("/camera/depth/points", 1, cloud_cb);
        marker_pub = nh.advertise<visualization_msgs::Marker> ("visualization_marker",1);
        ros::spin();
    }
    And its output is(which is not correct, since arr1 contaings only p0 which ranges from 2 to 100, it should come in order):
    Code:
    (1, 3, 48, 49, 30, 1715, 228, 228, 228, 228, 228, 228, 228) = 82
    (1, 3, 52, 69, 60, 1701, 228, 228, 228, 228, 228, 228, 228) = 72
    (1, 3, 61, 41, 50, 1715, 228, 228, 228, 228, 228, 228, 228) = 82
    (1, 3, 61, 59, 42, 1717, 228, 228, 228, 228, 228, 228, 228) = 57
    (1, 3, 62, 42, 59, 1716, 228, 228, 228, 228, 228, 228, 228) = 62
    (1, 3, 64, 44, 59, 1716, 228, 228, 228, 228, 228, 228, 228) = 62
    (1, 3, 66, 55, 46, 1703, 228, 228, 228, 228, 228, 228, 228) = 72
    (1, 3, 66, 82, 71, 1702, 228, 228, 228, 228, 228, 228, 228) = 62
    (1, 3, 70, 50, 64, 1715, 228, 228, 228, 228, 228, 228, 228) = 82
    And the main header file which contains the map "map.h" is:

    Code:
    #include <map>
    #include <iostream>
    #include <utility>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const string dimnam = "dimen.bin";
    const string dimnam1 = "dimen1.bin";
    const string dimnam2 = "dimen2.bin";
    const string dimnam3 = "dimen3.bin";
    struct dim {
        int dim1 = 0;
        int dim2 = 0;
        int dim3 = 0;
        int dim4 = 0;
        int dim5 = 0;
            int dim6 = 0;
            int dim7 = 0;
            int dim8 = 0;
            int dim9 = 0;
            int dim10 = 0;
            int dim11 = 0;
            int dim12 = 0;
            int dim13 = 0;
    
        dim() {}
        dim(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13) : dim1(d1), dim2(d2), dim3(d3), dim4(d4), dim5(d5), dim6(d6), dim7(d7), dim8(d8), dim9(d9), dim10(d10), dim11(d11), dim12(d12), dim13(d13) {}
    };
    
    class bigarray
    {
    public:
        int& operator()(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13)
        {
            return myarray[dim(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13)];
        }
    
        bool exists(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13) const
        {
            return myarray.count(dim(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13));
        }
    
        auto begin()
        {
            return myarray.begin();
        }
    
        auto end()
        {
            return myarray.end();
        }
    
        void clear()
        {
            myarray.clear();
        }
    
        void insert(const pair<dim, int>& pdi)
        {
            myarray.insert(pdi);
        }
    
        friend ostream& operator<<(ostream& os, const bigarray& ba);
        friend istream& operator>>(istream& os, bigarray& ba);
    
    private:
        map<dim, int> myarray;
    };
    
    bool operator<(const dim& ld, const dim& rd)
    {
        if (ld.dim1 < rd.dim1)
            return true;
    
        if (ld.dim1 == rd.dim1) {
            if (ld.dim2 < rd.dim2)
                return true;
    
            if (ld.dim2 == rd.dim2) {
                if (ld.dim3 < rd.dim3)
                    return true;
    
                if (ld.dim3 == rd.dim3) {
                    if (ld.dim4 < rd.dim4)
                        return true;
    
                    if (ld.dim4 == rd.dim4) {
                        if (ld.dim5 < rd.dim5)
                            return true;
    
                    if (ld.dim5 == rd.dim5) {
                        if (ld.dim6 < rd.dim6)
                            return true;
    
                    if (ld.dim6 == rd.dim6) {
                        if (ld.dim7 < rd.dim7)
                            return true;
    
                    if (ld.dim7 == rd.dim7) {
                        if (ld.dim8 < rd.dim8)
                            return true;
    
                    if (ld.dim8 == rd.dim8) {
                        if (ld.dim9 < rd.dim9)
                            return true;
    
                    if (ld.dim9 == rd.dim9) {
                        if (ld.dim10 < rd.dim10)
                            return true;
    
                    if (ld.dim10 == rd.dim10) {
                        if (ld.dim11 < rd.dim11)
                            return true;
    
                    if (ld.dim11 == rd.dim11) {
                        if (ld.dim12 < rd.dim12)
                            return true;
    
                    if (ld.dim12 == rd.dim12)
                        if (ld.dim13 < rd.dim13)
                            return true;
                                    } } } } } } } }
                }
            }
        }
        return false;
    }
    
    ostream& operator<<(ostream& os, const dim& pdi)
    {
        return os << "(" << pdi.dim1 << ", " << pdi.dim2 << ", " << pdi.dim3 << ", " << pdi.dim4 << ", " << pdi.dim5 <<  ", " << pdi.dim6 << ", " << pdi.dim7 << ", " << pdi.dim8 << ", " << pdi.dim9 <<  ", " << pdi.dim10 << ", " << pdi.dim11 << ", " << pdi.dim12 << ", " << pdi.dim13 <<")";
    }
    
    ostream& operator<<(ostream& os, const pair<dim, int>& pdi)
    {
        os.write((char*)&pdi.first, sizeof(pdi.first));
        os.write((char*)&pdi.second, sizeof(pdi.second));
        return os;
    }
    
    istream& operator>>(istream& is, pair<dim, int>& pdi)
    {
        is.read((char*)&pdi.first, sizeof(pdi.first));
        is.read((char*)&pdi.second, sizeof(pdi.second));
        return is;
    }
    
    ostream& operator<<(ostream& os, const bigarray& ba)
    {
        for (const auto& m : ba.myarray)
            os << m;
    
        return os;
    }
    
    istream& operator>>(istream& is, bigarray& ba)
    {
        pair<dim, int> pdi;
    
        ba.clear();
    
        while (is >> pdi)
            ba.insert(pdi);
    
        return is;
    }
    I feel like i am writing cout<<2*2<<endl; but the output is coming as 5. What is the problem in my program, the main object recognition program does loading of the object model in another program, i just loaded the file in the same program where i have written it to test it, first.
    Last edited by dinesh999lama; January 3rd, 2017 at 10:01 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: error using ifstream and osstream

    What output would you expect?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2016
    Posts
    24

    Re: error using ifstream and osstream

    Ok, got the problem it looks like somehow the key should not be same every time, which made the values of arr1 object un ordered. So i'll remove that now.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: error using ifstream and osstream

    Iterating bigarray will produce output sorted by dimension-value order (the same order as the operator< conditional tests). As its stands, you can't have output sorted by value - as that's not how a map stores its data. If you want the dimension-value order to be different, you need to change the order of the operator< conditional tests.

    Code:
    ofstream ofs(dimnam, ios::binary);
    ofs << arr; 
    ofs.close();
    
    ofstream ofs1(dimnam1, ios::binary);
    ofs1 << arr1;
    ofs1.close();
    
    ifstream ifs(dimnam, ios::binary);
    ifs >> arr;
    ifs.close();
    
    ifstream ifs1(dimnam1, ios::binary);
    ifs1 >> arr1;
    ifs1.close();
    Why are you first inserting into the output stream (saving) and then immediately extracting the same from the input stream (reading)? if you want to save just do the stream insertion. Doing an insertion followed by an extraction was just for testing in the example code to show that it worked.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2016
    Posts
    24

    Re: error using ifstream and osstream

    I was extracting them in same program just to test the concept first, its actual use is in another program where it extracts them and use them for object recognition task. This program only analyses the objects surface and than saves an object model, patterns of those above distance relations w.r.t each point in particular object.
    Last edited by dinesh999lama; January 5th, 2017 at 01:23 AM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: error using ifstream and osstream

    As the class is used in different programs with data being saved/read across them, it may be easier if the bigarray class included functions to directly save/read the matrix - rather than having the using program having to deal with streams. Consider
    Code:
    #include <map>
    #include <iostream>
    #include <utility>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const string dimnam = "dimen.bin";
    
    struct dim {
    	int dim1 = 0;
    	int dim2 = 0;
    	int dim3 = 0;
    	int dim4 = 0;
    	int dim5 = 0;
    
    	dim() {}
    	dim(int d1, int d2, int d3, int d4, int d5) : dim1(d1), dim2(d2), dim3(d3), dim4(d4), dim5(d5) {}
    };
    
    ostream& operator<<(ostream& os, const dim& pdi);
    
    class bigarray
    {
    public:
    	int& operator()(int d1, int d2, int d3, int d4, int d5)
    	{
    		return myarray[dim(d1, d2, d3, d4, d5)];
    	}
    
    	bool exists(int d1, int d2, int d3, int d4, int d5) const
    	{
    		return myarray.count(dim(d1, d2, d3, d4, d5));
    	}
    
    	auto begin()
    	{
    		return myarray.begin();
    	}
    
    	auto end()
    	{
    		return myarray.end();
    	}
    	
    	void clear()
    	{
    		myarray.clear();
    	}
    
    	void insert(const pair<dim, int>& pdi)
    	{
    		myarray.insert(pdi);
    	}
    
    	bool save(const string& fn) const
    	{
    		ofstream ofs(fn, ios::binary);
    
    		if (!ofs.is_open())
    			return false;
    
    		bool ret = !!(ofs << *this);
    		ofs.close();
    
    		return ret;
    	}
    
    	bool read(const string& fn)
    	{
    		ifstream ifs(fn, ios::binary);
    
    		if (!ifs.is_open())
    			return false;
    
    		ifs >> *this;
    		
    		auto ret = ifs.eof();
    		ifs.close();
    
    		return ret;
    	}
    
    	void display() const
    	{
    		for (const auto& m : myarray)
    			cout << m.first << " = " << m.second << endl;
    	}
    
    	friend ostream& operator<<(ostream& os, const bigarray& ba);
    	friend istream& operator>>(istream& os, bigarray& ba);
    
    private:
    	map<dim, int> myarray;
    };
    
    bool operator<(const dim& ld, const dim& rd)
    {
    	if (ld.dim1 < rd.dim1)
    		return true;
    
    	if (ld.dim1 == rd.dim1) {
    		if (ld.dim2 < rd.dim2)
    			return true;
    
    		if (ld.dim2 == rd.dim2) {
    			if (ld.dim3 < rd.dim3)
    				return true;
    
    			if (ld.dim3 == rd.dim3) {
    				if (ld.dim4 < rd.dim4)
    					return true;
    
    				if (ld.dim4 == rd.dim4)
    					if (ld.dim5 < rd.dim5)
    						return true;
    			}
    		}
    	}
    
    	return false;
    }
    
    ostream& operator<<(ostream& os, const dim& pdi)
    {
    	return os << "(" << pdi.dim1 << ", " << pdi.dim2 << ", " << pdi.dim3 << ", " << pdi.dim4 << ", " << pdi.dim5 << ")";
    }
    
    ostream& operator<<(ostream& os, const pair<dim, int>& pdi)
    {
    	os.write((char*)&pdi.first, sizeof(pdi.first));
    	os.write((char*)&pdi.second, sizeof(pdi.second));
    	return os;
    }
    
    istream& operator>>(istream& is, pair<dim, int>& pdi)
    {
    	is.read((char*)&pdi.first, sizeof(pdi.first));
    	is.read((char*)&pdi.second, sizeof(pdi.second));
    	return is;
    }
    
    ostream& operator<<(ostream& os, const bigarray& ba)
    {
    	for (const auto& m : ba.myarray)
    		os << m;
    
    	return os;
    }
    
    istream& operator>>(istream& is, bigarray& ba)
    {
    	pair<dim, int> pdi;
    
    	ba.clear();
    
    	while (is >> pdi)
    		ba.insert(pdi);
    
    	return is;
    }
    
    int main()
    {
    	bigarray arr;
    
    	arr(2, 3, 4, 5, 2) = 5;
    	arr(1, 1, 1, 1, 1) = 1;
    	arr(1, 1, 2, 2, 3) = 6;
    
    	cout << "arr(2, 3, 4, 5, 2): " << arr(2, 3, 4, 5, 2) << endl;
    	cout << "arr(1, 1, 1, 1, 1): " << arr(1, 1, 1, 1, 1) << endl;
    		
    	cout << "arr(1, 1, 1, 1, 1): " << (arr.exists(1, 1, 1, 1, 1) ? "Exists" : "Not exists") << endl;
    	cout << "arr(2, 2, 2, 2, 2): " << (arr.exists(2, 2, 2, 2, 2) ? "Exists" : "Not exists") << endl << endl;
    
    	if (!arr.save(dimnam)) {
    		cout << "Error saving data to file " << dimnam << endl;
    		return 1;
    	}
    
    	if (!arr.read(dimnam)) {
    		cout << "Error reading data from file " << dimnam << endl;
    		return 2;
    	}
    
    	arr.display();
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Nov 2016
    Posts
    24

    Re: error using ifstream and osstream

    The first one solution u provided me was the one which i was looking for. exactly that for now, since i need to save the object model in a file to use it in another program any time, i need to extract those datas saved and than use them for pattern matching. so thank u. it is also running smoothly than previous methode (rosbag) which i was using.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured