LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-16-2008, 10:39 PM   #1
HBMSGUY
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
How to input values from *.txt to an array in C++


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.

Thanks so much, it really helps.
 
Old 11-17-2008, 01:08 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Rep: Reputation: Disabled
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.

Last edited by Nylex; 11-17-2008 at 11:45 PM.
 
Old 11-17-2008, 07:01 PM   #3
HBMSGUY
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks.

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.

Last edited by HBMSGUY; 11-17-2008 at 07:59 PM.
 
Old 11-17-2008, 11:37 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Rep: Reputation: Disabled
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).

Last edited by Nylex; 11-17-2008 at 11:47 PM.
 
Old 11-17-2008, 11:51 PM   #5
HBMSGUY
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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.

Last edited by HBMSGUY; 11-17-2008 at 11:55 PM.
 
Old 11-18-2008, 12:22 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Rep: Reputation: Disabled
Quote:
Originally Posted by HBMSGUY View Post
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.
Yes and yes and no problem .
 
Old 11-18-2008, 09:33 PM   #7
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 70
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plz need help on C++ about read int from a TXT to array. cyborgt Programming 5 02-20-2008 02:11 PM
extracting values from an array bahadur Programming 2 08-14-2006 01:07 AM
dns2.txt file has stale dns values jkaslick Linux - Software 1 06-30-2006 09:35 AM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM


All times are GMT -5. The time now is 03:28 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration