# Txt file and data storage

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 06-15-2008
radnik
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!!
• 06-15-2008
anon
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);     }     //... }```
• 06-15-2008
zidsal
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.
• 06-15-2008
radnik
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
• 06-15-2008
anon
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.
• 06-15-2008
radnik
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.
• 06-15-2008
anon
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```
• 06-16-2008
radnik
Thank you for everything anon. You've been a great help.
• 06-16-2008
radnik
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.
• 06-16-2008
MacGyver
size().
• 06-16-2008
laserlight
This looks very familiar.

Quote:

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.
• 06-16-2008
radnik
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.
• 06-16-2008
laserlight
Quote:

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?

Quote:

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.
• 06-16-2008
radnik
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.
• 06-16-2008
radnik
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::operator<<............

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; }```
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last