Txt file and data storage

This is a discussion on Txt file and data storage within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by radnik Can someone please help me with this stringstream. Just give me a good link if you ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by radnik View Post
    Can someone please help me with this stringstream. Just give me a good link if you know it or explain it to me how to parse integers from string with it please.
    A stringstream works just like an input or output stream. So we could do something like this:
    Code:
    #include <sstream>
    
    int main()
    {
        std::stringstream ss;
    
        ss << "100 200 300";
    
        int x;
        while(ss >> x)
            std::cout >> x >> std::endl;
        return 0;
    }
    This would give you an output of
    100
    200
    300

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    The larch
    Join Date
    May 2006
    Posts
    3,573
    stringstreams are a kind of streams, therefore the interface and usage is practically the same as cout, cin and fstreams.

    The difference from file streams is that they don't work on a file, but on a string. So, in the constructor you don't specify a file name but a string.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s("10 23 -3 18 9");
        stringstream ss(s);
        int n;
        while (ss >> n) {
            std::cout << n << '\n';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #33
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Oh, i see. Thank you guys very much.

  4. #34
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    I'm stuck again. Now when i do something like:
    Code:
    string tmp;
    		
    getline(dat, tmp);
    stringstream stmp(tmp);
    i have stringstream stmp with one line of numbers from txt file. But how do i read stringstream and store data in x and y coordinates. I understand the simple example that you gave me, but have no idea on how to do this
    Is there a way to test end of stringstream in wile loop or something similar??
    Last edited by radnik; 06-19-2008 at 12:32 PM.

  5. #35
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This line in both mine and matsp's example tests for end of stringstream in exactly the same way you'd check for end of user input with std::cout (Ctrl + Z) and end of file if simply reading integers from a file.

    Code:
    while (ss >> n)
    You can read two variable at a time:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s("10 23 -3 18 9 100 83 29 1 -1");
        stringstream ss(s);
        int n, m;
        while (ss >> n >> m) {
            std::cout << n << ' ' << m << '\n';
        }
    }
    However, it is somewhat hard to detect problems while parsing the line (e.g odd number of values), so it just assumes original input is good.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #36
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Thanks, that helped a lot. What do you suggest me to do on writing numbers of one path in vector Path. Since i don't set it's size (for logical reasons) i can't use something like:
    Code:
    while (stmp >> x >> y) {
    			
    			//cout <<"broj is smtp"<<br<<"referenca od p"<<pp<<endl;
    			
    				t[pp].x=x;
    				t[pp].y=y;
    				pp++;
    	}
    What would be my other options? I know how i can count lines in text and in that way set the size of vector Collection of paths. But how can i count numbers per line?? As a matter of fact even if i could count them not every line has the same amount of numbers so this approach is no good. Suggestions please.
    Last edited by radnik; 06-19-2008 at 02:09 PM.

  7. #37
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The good thing about vectors is that you don't need to know how much data you are going to store beforehand: push_back.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #38
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    I know that, but when i read two values x and y how can i do bush back. How can i know that it will read the x value in vetor.x and y in vector.y?So when i read the values from file like this:
    Code:
    while (stmp >> x >> y) {
    			
    				//cout <<"broj x	"<<x<<"	broj y	"<<y<<"	broj pp	"<<pp<<endl;
    how can i store it in vector of classes Koord?:
    Code:
    class Koord {		//varijabla vrijednosti koordinatnih tocaka x i y
    
    	public:
    		double x;
    		double y;
    		
    	};
    	typedef vector<Koord>Tocke;		//skup tocaka jednog puta
    	typedef vector<Tocke>Putovi;		//svi putovi mape
    I know that i will fill "collectionofpaths" with trajektorije.push_back(t); (Putovi trajektorije;, Tocke t), as you said me to do it earlier in this topic.
    Last edited by radnik; 06-21-2008 at 02:33 AM.

  9. #39
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Guys i'm really desperate here. I'm obviously doing something wrong and i don't know what. I post the code and you compile it and look at the output. I have no clue what is the problem and i'm sure that it's something trivial but i don't have the experience to notice it. When i try to print the whole trajektorije vector which is collectionofpaths i get some random memory junk. And is still have the problem i mentioned in the earlier post above. So please if anyone could help me i would be grateful.Here's the code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class Koord {		//varijabla vrijednosti koordinatnih tocaka x i y
    
    	public:
    		double x;
    		double y;
    		
    	};
    	typedef vector<Koord>Tocke;		//skup tocaka jednog puta
    	typedef vector<Tocke>Putovi;		//svi putovi mape
    
    int main(int argc, char* argv[])
    {
    	
    	Putovi trajektorije(15);
    	Tocke t(50);
    	
    	if (argc == 2) {		//ako je pri pozivu naveden samo jedan parametar stvara se ulazni tok dat i pridruzuje datoteci zadanoj kao parametar
    	
    	ifstream dat (argv[1]);
    	if (!dat) {
    		cerr << "Nije moguce otvoriti zadanu datoteku "<< argv[1]<<endl;
    	}
    	
    	while (!dat.eof()) {
    		
    		string tmp;
    		double x,y;
    		
    		getline(dat, tmp);
    		stringstream stmp(tmp);
    		//cout << "Linija:"<<tmp<<endl;
    		int p=-1;
    		int &pp = p;
    		
    		while (stmp >> x >> y) {
    				
    				p++;
    				cout <<"brojac pp="<<pp<<"\n"<<"broj x	"<<x<<"	broj y	"<<y<<endl;
    			
    				t[pp].x=x;
    				t[pp].y=y;
    				cout <<"broj x unutar vektora	"<<t[pp].x<<"	broj y unutar vektora	"<<t[pp].y<<endl;
    	}
    
    		
    		trajektorije.push_back(t);
    		//t.clear();
    	}	
    
    	
    
    }	
    else if (argc < 2) {
    			cout << "Greska pri pozivanju programa" <<endl;
    			cout << "Koristenje: eg_decomposition <ime_datoteke>" <<endl;
    		}
    		else {
    			cerr << "Zadano je previse parametara u pozivu programa" << endl;
    			exit (1);
    		}	
    		
    		ifstream dat (argv[1]);
    		dat.clear();
    		dat.seekg(0);
    		
    		string linija;
    		int s=0;
    		int &size = s;
    		
    		while (getline (dat,linija)) {
    			size++;
    		}
    		s=size;
    		trajektorije.resize(s);
    		
    		int i,j;
    		for(i=0; i < trajektorije.size(); i++) {
    			for (j=0; j < t.size(); j++)
    				cout << '\t' << trajektorije[i][j].x <<' '<<trajektorije[i][j].y << endl;
    	}
    	
    	return 0;
    }

  10. #40
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Anyone??

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I suggest that you format your code properly, and more importantly, use English. You can always convert them to your natural language later, but when asking for help in a message board community that is pre-dominantly English, write in English.

    It would also help if you divided your code into well named functions. This would allow you to test a particular function to see if it is working, and thus become confident that if there is a problem, it most likely lies elsewhere. This divide and conquer approach will help you debug your code, and help others understand it. Likewise, use meaningful variable names, not t and p.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I may have gotten lost, but why do you resize trajektorije after all the data has been read in?

    And for that matter, why bother with the s and the size -- since size is a reference to s, they will always and forever refer to the same variable, so there's no need for both.

  13. #43
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    	ifstream dat (argv[1]);
    	if (!dat) {
    		cerr << "Nije moguce otvoriti zadanu datoteku "<< argv[1]<<endl;
    	}
    I think you want "if(!dat.is_open())" there.

    Anyway. I'm not too sure what you're trying to do -- I haven't read the whole thread -- but perhaps this will be helpful:
    Code:
    #include <iostream>
    #include <vector>
    
    class pos {
    public:
        int xp, yp;
        pos() {}
        pos(int x, int y) : xp(x), yp(y) {}
    };
    
    int main() {
        std::vector<pos> v(10);
        v.push_back(pos(2, 3));
        v.push_back(pos(5, 4));
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #44
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    You are right laserlight. Sorry for the Croatian language. I will post English version now. I don't know what you mean by formatting of code because all of the "layout" was done by my editor i didn't touch a thing. I will put all of the code in functions later.
    Dwks thank you. That helped a lot. But still while printing values of paths vector i get some memory junk in between my real values. Like this:
    Code:
     
            12 14
            15 17              PATH 1
            987 234
            23 45
    
            
             3.60739e-313 4.95672e-270
            1.54972e-312 12
            14 15
            17 987
            234 23                           SOME MEMORY JUNK AND REPEATED VALUES FROM PATH 1
            12 14
            15 17
            987 234
            23 45
    
            
            1 2
            65 34
            56 34                     PATH 2
            12 78
            23 987
    I'll post new code with English and deleted unnecessary parts:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class Coord {		//values of coordinates (x,y)
    
    	public:
    		double xt;
    		double yt;
    		
    		Coord(){};
    		Coord(double x, double y) : xt(x), yt(y) {}; 
    		
    	};
    	
    typedef vector<Coord>Dots;		//collection of coordinates for one path (one line in txt file is one path)
    typedef vector<Dots>Paths;		//collection of all paths
    
    int main(int argc, char* argv[])
    {
    	
    	Paths trajectories;
    	Dots pairs;
    	int i,j;
    	
    	if (argc == 2) {	
    	
    	ifstream dat (argv[1]);
    	if (!dat) {
    		cerr << "Given file cannot be opened "<< argv[1]<<endl;
    	}
    	
    	while (!dat.eof()) {
    		
    		string tmp;
    		double x,y;
    		
    		getline(dat, tmp);
    		stringstream stmp(tmp);
    
    		int p=-1;
    		int &pp = p;
    		
    		while (stmp >> x >> y) {
    				
    				p++;
    				cout <<"counter pp="<<pp<<"\n"<<"number x	"<<x<<"	number y	"<<y<<endl;
    			
    				pairs.push_back(Coord(x,y));
    
    				cout <<"number x inside vector	"<<pairs[pp].xt<<"	number y inside vector	"<<pairs[pp].yt<<endl;
    				// the lines for printing are just for testing because i had some problems here
    		}
    		
    		trajectories.push_back(pairs);	
    	}	
    }	
    else if (argc < 2) {
    		cout << "Error while running program" <<endl;
    		cout << "Usage: eg_decomposition <file_name>" <<endl;
    	}
    else {
    		cerr << "Too much parameters" << endl;
    		exit (1);
    	}	
    		
    // printing of all paths		
    for(i=0; i < trajectories.size(); i++) {
    	for (j=0; j < pairs.size(); j++)
    		cout << '\t' << trajectories[i][j].xt <<' '<<trajectories[i][j].yt << endl; 
    				
    	}
    	
    return 0;
    }
    Last edited by radnik; 06-23-2008 at 02:41 AM.

  15. #45
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    If i'm right the repeating of numbers occurs because i never clear my pairs vector so those old coordinates remain in the vector. How can i solve this? I've tried this:
    Code:
    while (stmp >> x >> y) {
    				
    				p++;
    				cout <<"counter pp="<<pp<<"\n"<<"number x	"<<x<<"	number y	"<<y<<endl;
    			
    				tmp_pairs.push_back(Coord(x,y));
    
    				cout <<"number x inside vector	"<<tmp_pairs[pp].xt<<"	number y inside vector	"<<tmp_pairs[pp].yt<<endl;
    				// the lines for printing are just for testing because i had some problems here
    		}
    		
    		pairs=tmp_pairs;
    		trajectories.push_back(pairs);
    		tmp_pairs.clear();
    But then when i tried to print the values i got nothing. Why is this happening if i cleared the temporary vector tmp_pairs? I presume that i can't do something like pairs=tmp_pairs;. How could i do this the other way? I tried the vector copying method with vector<Coord> pairs(tmp_pairs) but still got the same results Is there any method of appending values of one vector to another?
    Last edited by radnik; 06-23-2008 at 04:22 AM.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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