ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello I am looking for a way to take data from a *.txt file, as part of my bigger project. The file looks like this:
1 0.0 0.0
2 10.1 -10.1
3 -12.2 12.2
4 38.3 38.3
5 79.99 179.99
Now I don't mean to be picky, but I want it to go into an array of doubles like this.
originalData[0][0] = 1
originalData[0][1] = 0.0
originalData[0][2] = 0.0
originalData[1][0] = 2
originalData[1][1] = 10.1
and it continues.
There is one last thing: The file will always be in this format with each line being complete, although I don't know till the time of execution how many lines there will be.
Please make sure you choose your forum more carefully in future!
Two things I can think of for doing this:
1. Go through the file, checking how many lines there are and then create your array based on that.
2. Just put all the numbers into a C++ vector (they're like arrays, but can grow and shrink) and since you know each line in the file contains 3 values, dividing the size of the vector by 3 will give you the number of lines..
There may be other, better ways of approaching your problem, but these are just the ways I could come up with.
Thank you very much for the reply, that is what looks like to me a good way to store the data. I am sorry I got this in the wrong category, I could have sworn I picked programming. But, I still I am not understanding how to take the information from the file. I have looked at the <ifstream> but the when I use get, it get() it gives me two characters. And read will not read just a character.
Thanks for the help.
Do you know how to read input from the console using cin? If so, you can read from a file in the same way, but using an ifstream object, e.g.
Code:
ifstream in("file.txt");
double d;
// Loop will break when there are no more values to read
while(in >> d)
{
// Do stuff
}
Edit: This will read individual values. If you want entire lines, then use getline() (but then you'd have the issue of splitting that line up into its constituent elements).
Thanks, I am starting to see that I was over thinking the input task. I am guessing that the way the >> operator works is it will just read the next value. Then next time the loop repeats it will read the numeric value after, and so on. Thanks so much, I really appreciate the help.
Thanks, I am starting to see that I was over thinking the input task. I am guessing that the way the >> operator works is it will just read the next value. Then next time the loop repeats it will read the numeric value after, and so on. Thanks so much, I really appreciate the help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.