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

    Txt file reading and data storing

    Hey guys i need some help. I have a txt file which looks like this:
    234 56 123 34 456 56 7 45.....
    123 3 34 56 56 67 67 2.....
    .
    .
    .
    Where each line represents a series of points in coordinate system, and the first number in each line represents the x-point, second y-point,third x-point and so on. I need to read the file, and store the data. I would like to store it in a structure:
    struct path {

    int x();
    int y();
    };
    I would also need some kind of automatic variable naming for struct path, so when i begin to store second line of points i would have struct name path2. These are just my ideas, so if you can, please help with every approach you can think of. And when the data storing is done i should be able to know the total number of paths and total number of point pairs (x,y) for every path. If you have any ideas please help, i'm a begginer and really need to solve this problem. Thank you in advance!!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Txt file reading and data storing

    I need to read the file, and store the data. I would like to store it in a structure
    That's fine, though you could also consider using a std::pair. However, your example declares a struct named path with two member functions. You would write instead:
    Code:
    struct path {
        int x;
        int y;
    };
    Of course, you would need to store a list of path objects that corresponds to each line of data.

    I would also need some kind of automatic variable naming for struct path, so when i begin to store second line of points i would have struct name path2.
    An array or a std::vector of a list of path objects would be appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2008
    Posts
    4

    Re: Txt file reading and data storing

    Thanks for the reply laserlight. I made a mistake in my first post. I meant:
    struct path {

    int x[];
    int y[];
    };
    because i have multiple x and y points for every path. How would i use std:air, considering i have multiple points for every path?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Txt file reading and data storing

    Yes, that does make sense. However, then the naming should just change. A path is a series of connected points. A point has a pair of coordinates. Consequently, you could have a Path class that stores a list (e.g., a std::list, std:eque or std::vector) of Point objects. The Point class could be like my example for path, or it could be a typedef for std::pair (but having member variables named x and y would make things easier to understand).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2008
    Posts
    4

    Re: Txt file reading and data storing

    Ok, i'll try it with my struct. So what you are suggesting is to have array of structures like:

    struct path {

    int x[];
    int y[];
    };

    path paths[i];

    Am i right? I'm sorry for bugging you but i have more questions. I'm not to familiar with c++ so would you please tell me how would i read i line of numbers in txt file, store them in my structure and recognize EOL?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Txt file reading and data storing

    No, more like:
    Code:
    struct Point
    {
        int x;
        int y;
    };
    
    class Path
    {
    public:
        // ...
    private:
        std::list<Point> points;
    };
    Variable length arrays are not part of standard C++.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2008
    Posts
    4

    Re: Txt file reading and data storing

    If i have class and two vectors as follows:
    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
    
            Putovi trajektorije(15);
    	Tocke t(50);
    then i read one line of text in stringstream and read doubles from stringstream:
    Code:
    while (!dat.eof()) {
    		
    		string tmp;
    		double x,y;
    		
    		getline(dat, tmp);
    		stringstream stmp(tmp);
    		//cout << "Linija:"<<tmp<<endl;
    		int p=0;
    		int &pp = p;
    		
    		while (stmp >> x >> y) {
    how can i store x and y point in vector "Tocke t"? Is it possible to do it with push_back? I have no idea on how to do this. If i set the size of vector (and i wouldn't like to because i then loose the meaning of using vectors) the whole vector will be filled with zeroes or some random memory junk. Is there any other way of doing this?

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