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; Hey guys i need some help. I have a txt file which looks like this: 234 56 123 34 456 ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    33

    Txt file and data storage

    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the length of each line and number of lines is unknown, you can't use stack arrays, but need dynamically allocated arrays. In C++, dynamically allocated arrays are managed by the std::vector class.

    It might also be wise to keep the x and y value together, rather than in different arrays.

    Code:
    struct Point
    {
        int x, y;
    };
    
    typedef std::vector<Point> Path;
    typedef std::vector<Path> PathCollection;
    
    int main()
    {
         PathCollection paths;
         while (...) {
              Path p;
              //fill up Path
              paths.push_back(p);
         }
         //...
    }
    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. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    well my way is proberly the most basic way to do it (its not even in binary).

    first of all set it so only 1 number is on 1 line i.e
    272 //x
    729 //y
    383 //x

    Code:
    int main()
    {
    string strHoldData; // uses to put fileinput in here
    int  intHoldData; // used so you can convert the string into a integer
    int holdData[10]; // holds the x and y points
    int arrayIncrease = 0; //used to put points in respected array slots
    
    ifstream readCord(file name here);
     
    do{
             getline(readCord,strHoldData;
             intHoldData atoi(strHoldData.c_str())
             holdData[arrayIncrease] = intHoldData;
             holdData++;
    }(while !strHoldData = "")
    
    }
    this remains untested the result will be that Holddata[0] will hold the first x value and HoldData[1] will hold the first y value. E.T.C.

    I don't reccomend using this as I am unexperieanced and this is proberly the worst way to go about it.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Thanks for the reply anon. I'm really not familiar with the vector class and i'm new to c++ so could you please tell me how can i access single item stored in vector.I think that this solution isn't good because i need to know which point is x and which y in the coordinate system. Thats why i proposed structure with two array elements. I'm probably wrong so please explain it to me. Thanks

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    x is Point.x and y is Point.y. Could it be any simpler?

    Vector elements are accessed with operator[] just like arrays, but unlike arrays you can safely add elements to the end of the array with 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).

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Ok , that indeed is simple. So for example point[0] would represent first two pairs of x and y. I could then access them with point[0].x. Am i right? There's just one more problem. When i read the file first line is for the first path, second line is for the second path etc. Are there any means by which i could name my vectors automatically (like path1, path2...) because i don't know how many lines are there in txt file?
    Maybe it will be clearer to you when i explain the whole problem. As i already mentioned lines in txt file represent paths, and numbers in lines represent dots in coordinate system (x,y). I need to read the values and store them. I need this because i will later use the coordinates of every path for robot motion. So when i'm finished with file reading and storing i should be able to get every coordinate (pair (x,y)) of every path to move my robot. I hope that this clears my problem to you, and i'm really sorry for bugging you.
    Last edited by radnik; 06-15-2008 at 04:21 PM.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't and you don't need to "create names" on runtime. Instead (and that is every time you wish for a way to create names with numbers appended to them) you use an array (or vector).

    In my example I showed some typedefs. The second one is for a vector of vectors of Points. The point of the typedefs is that they can shorted otherwise very long typenames. Instead you could also type:

    Code:
    std::vector<Point> one_path;
    std::vector<std::vector<Point> > lots_of_paths;
    You are right about how you would access the coordinates in a single path. In a collection of paths the syntax would be similar to two-dimensional arrays:
    Code:
    unsigned path_no = 10;
    unsigned point_no = 12;
    
    lots_of_paths[path_no][point_no].x = 42;

    Or create a reference to a single path if we need to do lots of things with it:
    Code:
    std::vector<Point>& interesting_path = lots_of_paths[42];
    interesting_path[10].y = 3;
    
    //because interesting_path is a reference the last line affects
    //lots_of_paths[42][10].y
    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. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Thank you for everything anon. You've been a great help.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    It's me again. lots_of_paths[0] stands for my first path, lots_of_paths[1] for second and so on (if i'm not mistaking). How can i find out the number of complete paths in lots_of_paths. Is there a way to find out number of elements in vector? And what function should i use for reading file and storing numbers? get(ch), or what? I need something that ignores spaces.
    Last edited by radnik; 06-16-2008 at 03:16 AM.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    size().

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    This looks very familiar.

    Is there a way to find out number of elements in vector?
    Yes, by using the size() member function.

    I disagree with the use of a typedef here. You should create a Path class, as noted in my reply at CodeGuru.
    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. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Hey laserlight, i didn't use classes because i'm not to familiar with them and i really don't have time to study them now. Here's what i've done so far:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    class Koord {		//varijabla vrijednosti koordinatnih tocaka x i y
    		int x,y;
    	};
    	typedef vector<Koord>Tocke;		//skup tocaka jednog puta
    	typedef vector<Tocke>Putovi;		//svi putovi mape
    
    int main(int argc, char* argv[])
    {
    	Putovi trajektorije;
    	Tocke t;
    	int i,j;
    	
    	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;
    	}
    	int br;
    	while (dat.get(br)) {
    		if (br == '\n') 
    		trajektorije.push_back(t);
    			
    		t.push_back(br);
    	}
    	
    	for(i=0; i < trajektorije.size(); i++) {
    		for (j=0; j < t.size(); j++)
    	cout << "Put"<<i<<" se sastoji od slijedecih tocaka"<<trajektorije[i][j]<<endl;
    	}
    }
    I wanted to compile it to test the output, but i got this error:
    eg_decomposition.cpp: In function 'int main(int, char**)':
    eg_decomposition.cpp:48: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::get(int&)'.

    Sorry for Croatian comments in the code but i think that you will get it.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    Hey laserlight, i didn't use classes because i'm not to familiar with them and i really don't have time to study them now.
    This is a good opportunity to learn how to use classes properly. If you do not have the time to study them now, what do you have time for?

    I wanted to compile it to test the output, but i got this error:
    eg_decomposition.cpp: In function 'int main(int, char**)':
    eg_decomposition.cpp:48: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::get(int&)'.
    It looks like get() is not what you want to use. You want to use formatted input with dat >> br instead.
    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

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Ok laserlight, i will do it the right way and try to do it with classes. I just don't understand how do you suggest me to store complete paths in the class you suggested.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    I have one more problem with printing vector values. I get the following error when compiling:
    eg_decomposition.cpp: In function 'int main(int, char**)':
    eg_decomposition.cpp:66: error: no match for 'operator<<' in 'std:perator<<............

    Code:
    for(i=0; i < trajektorije.size(); i++) {
    	for (j=0; j < t.size(); j++)
    	cout << "Put #" << j+1 << " se sastoji od slijedecih tocaka:" << '\t' << trajektorije[i][j] << endl;
    }

Page 1 of 4 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