Reading in data in columns from a file (C++)
Hi people, I have a file with some data in columns (separated by tabs) like so:
Code:
1.23 1.354 2.78 Any ideas on how to do what I need? Thanks! |
well if you know the structure of the file is always going to be like that, you can use the stream extractor >> operator with ifstream
ex Code:
double cola, colb, colc; |
<edit> My browser hadn't refreshed so I didn't see xhi's post.
</edit> Please not this code expects you to know the number of cols and rows and does not delete the memory, but it shows you how to do what you ask. Code:
#include <fstream> 1.23 1.354 2.78 1.44 10.023 2.756 2.31 20.3 10.07 output 1.23 1.354 2.78 1.44 10.023 2.756 2.31 20.3 10.07 |
Thank you both for the replies.
xhi, your example does pretty much what I need (with a slight modification to use arrays and stuff, but that was easy enough for me to do). I don't know why I thought that doing that wouldn't work. I was trying to use getline() and stuff. dmail, I didn't really understand your code to be honest and I've never had to use double pointers before (I assume that's what you call them, but obviously I could be wrong). However, thank you again for taking the time to write it as it is appreciated! |
Good to hear it that you have have sorted Nylex. I would never have written the code if I had seen xhi's post, but maybe i should add some comments so that anyone in future can understand it.
Code:
#include <fstream> |
All times are GMT -5. The time now is 11:39 AM. |