473,385 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reading x,y coordinates in text file c++

hi i need to read x,y coordinates in text file using c++
the name of file is input.txt which is ,
Expand|Select|Wrap|Line Numbers
  1. x      y
  2. 232.1,753.7
  3. 100.5,981.6
  4. 96.2,992.9
  5. 148.7,953.2
  6. 197.2,999.3
  7. 249.9,998.6
  8. 261.7,975.9
  9. 262.4,905.8
  10. 334.9,980.8
  11. 371.6,979.4
  12. 396.7,996.3
  13. 405.1,995
  14. 565.5,766
  15. 459.4,988.5
  16. 474.4,994.6
  17. 594.6,996.8
  18. 604,987.3
  19. 612.8,877.3
  20. 664.1,992.6
please help me the following code was did not work. any other code is appretiated
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std
  4.  
  5. struct monsters_struct {
  6. int x, y;
  7. };
  8.  
  9. int main()
  10. { monsters_struct monsters[100]; //Create an array "monsters" as type "monsters_struct"
  11. ifstream inData("input.txt");
  12. for (int i=0; i<20; i++) {
  13. inData >> monsters[i].x >> monsters[i].y; //Read x and y coordinates at position i
  14. cout << monsters[i].x << " " << monsters[i].y << endl; //Output the x and y coordinates read above
  15. }
  16. inData.close();
  17. return 0;
  18. }
Apr 7 '14 #1

✓ answered by divideby0

Expand|Select|Wrap|Line Numbers
  1. inData >> monsters[i].x >> monststers[i].y;
  2.  
  3. // x  y (bad data)
  4. // 232.1,753.7  (good data, but wrong types)
  5.  
I believe the above code would fail when it encounters the wrong type of the comma since your file's data is float char float - eg: 123.4,567.8

Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2. {
  3.     float x;
  4.     float y;
  5.  
  6. } monsters_struct;
  7.  
  8. char delim;
  9. monsters_struct monsters[100] = { 0 };
  10. ...
  11.  
  12. inData >> monsters[i].x 
  13.        >> delim
  14.        >> monsters[i].y
  15.  
If your data file has "x y" as the first line, then you'll want to skip reading that into your struct.

If your file has the floating numbers, change x and y to floats or doubles.

1 12326
divideby0
131 128KB
Expand|Select|Wrap|Line Numbers
  1. inData >> monsters[i].x >> monststers[i].y;
  2.  
  3. // x  y (bad data)
  4. // 232.1,753.7  (good data, but wrong types)
  5.  
I believe the above code would fail when it encounters the wrong type of the comma since your file's data is float char float - eg: 123.4,567.8

Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2. {
  3.     float x;
  4.     float y;
  5.  
  6. } monsters_struct;
  7.  
  8. char delim;
  9. monsters_struct monsters[100] = { 0 };
  10. ...
  11.  
  12. inData >> monsters[i].x 
  13.        >> delim
  14.        >> monsters[i].y
  15.  
If your data file has "x y" as the first line, then you'll want to skip reading that into your struct.

If your file has the floating numbers, change x and y to floats or doubles.
Apr 7 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
1
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
2
by: bambataa | last post by:
Hellloww...i am new to Java and i am facing a problem, i am reading a text file, i am using string tokenizer class,..now at some point when i read a first token i have to check it if its an integer...
1
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 ...
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
2
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: xakzyy | last post by:
Hello peeps! I need some help to decrypt my text file and make it able to read in my program.. What I have programmed so far is to read the encrypted file, create a new file, decrypt it and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.