inputing a text file into an array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved
  View First Unread View First Unread

Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: inputing a text file into an array

 
0
  #11
Jan 27th, 2008
you don't need lines 25-29 to zero out the array. Do it when the array is declared, like this:
int matrix[101][5] = {0};
>>What alterations/additions do I make to the code to import the .txt file into the array?
Code that reads the text file. Start by deleting lines 1 to 3 -- al those files are obsolete in C++. Most likely all you need is this:
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;

Next declare an ifstream object Then in a loop read the integers into the array. See Vernan's post #6 above, which is an example of how to read the file.
Last edited by Ancient Dragon; Jan 27th, 2008 at 6:09 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: inputing a text file into an array

 
0
  #12
Jan 27th, 2008
Sadly, the c++ compiler i have to use at uni must use those headers, as it isn't a very new program. So I do have to use them

You say to declare an ifstream object and then in a loop, read the integers into an array.

I know this is what I must do, but this is the code I am struggling with... I've never come across any examples of this before. Could you possibly give me some brief code that will read the integers into the array?

Thanks for the help with zeroing, that worked a treat
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: inputing a text file into an array

 
0
  #13
Jan 27th, 2008
So you do know that you have three columns? You don't know how many rows, but there are three columns? If you don't know how many columns, this won't work. If it's different from 3, change numCols. Anyway, here's a way to read it in:

  1. int numCols = 3;
  2. int numRows = 0;
  3.  
  4.  
  5. while (inFile >> matrix[numRows][0])
  6. {
  7. printf ("%d\t", matrix[numRows][0]);
  8. for (j = 1; j < numCols; j++)
  9. {
  10. inFile >> matrix[numRows][j];
  11. printf ("%d\t", matrix[numRows][j]);
  12. }
  13. numRows++;
  14. printf ("\n");
  15. }
  16.  
  17. inFile.close ();

You have
  1. inFile.close ();

at the top before anything is read in, so you want to take that out. I think these lines are going to give you an error:

  1.  
  2. printf("\t%d", matrix[i][1],k);
  3. printf("\t%d", matrix[i][2],k);

You only have one "%d", but you have two parameters. I'd take the "k" parameter out.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: inputing a text file into an array

 
0
  #14
Jan 27th, 2008
That works perfectly, thank you so much!!

Ok, for my next question:

I want there to be three columns for the text file yes. HOWEVER, i want to have 2 further columns with empty data in them, ready to put calculations in. Is this possible?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: inputing a text file into an array

 
0
  #15
Jan 27th, 2008
Originally Posted by andyg55 View Post
Sadly, the c++ compiler i have to use at uni must use those headers, as it isn't a very new program. So I do have to use them
Oh, that's unfortunate that your school is making you learn something that will never be used in the real world. We tossed out those compilers 15 years ago.

Originally Posted by andyg55 View Post
I've never come across any examples of this before.
Well, then you didn't read the POST #6 very well, if at all, because it has exactly what you need.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 35
Reputation: CodyOebel is an unknown quantity at this point 
Solved Threads: 2
CodyOebel CodyOebel is offline Offline
Light Poster

MY bad

 
0
  #16
17 Hours Ago
Dunno what I was thinking at the time.. please forgive me.
'


Originally Posted by Ancient Dragon View Post
Cody: if you want to post code, that is ok, but please
  • Use code tags [code=c++] /* code goes here */ [/code]
  • Don't use ancient header files with .h extension
  • Don't use eof() as you did in line 12 because it doesn't work like that

>> This reads the text in as characters
Why? it could read into integer variables just as easy. By reading in as strings the strings have to be converted into ints before they can be used. That just extra unnecessary work.
Name: Cody Oebel
Place : San Antonio , TX.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
0
  #17
17 Hours Ago
Originally Posted by CodyOebel View Post
Dunno what I was thinking at the time.. please forgive me.
'
Nothing like a two-year late response
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC