inputing a text file into an array

Thread Solved
Reply

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

inputing a text file into an array

 
0
  #1
Jan 26th, 2008
Hi,

I'm trying to create a 2d array that will fill itself up with numbers i have in a text file.

My text file looks like this:

1 5 4
2 6 3
3 9 4
4 1 8
5 2 2

So, I want to create an array with 5 rows and 3 columns that will print this exact output to the screen.

Could anybody help me with the code please?

Thank you
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,839
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 49
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: inputing a text file into an array

 
0
  #2
Jan 26th, 2008
>> Could anybody help me with the code please?
Not unless you show some work.
But it's not that hard. There are two problems to overcome -- 1) filling the array 2) reading values from a file. Once you have those two done there's really no problem. Show attempts on both.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 29
Reputation: CodyOebel is an unknown quantity at this point 
Solved Threads: 2
CodyOebel CodyOebel is offline Offline
Light Poster

Re: inputing a text file into an array

 
-1
  #3
Jan 26th, 2008
  1. #include <iostream.h>
  2. #include <fstream.h>
  3.  
  4. int main()
  5. {
  6. /* open the file to read from in this case being your text file*/
  7. char chars[50];
  8. char newline = '\n'; // for the get line function's 3rd argument .. acts as a terminating delimeter.
  9.  
  10. ifstream in("c:\\documents and settings\\your profile name\\desktop\\theNameOftheTXTfile.txt");
  11.  
  12. while(! in.eof())
  13. {
  14. in.getline(chars,50,newline);
  15. cout<<chars<< endl;
  16. };
  17. system("pause"); // I use DEV as im a hobby
  18. //programmer.
  19. return 0;
  20. };
I didnt compile this code.. should work, if not post the error, and I will get back to this post later.

NOTE: This reads the text in as characters, so although they are numbers in the note pad, they will be read in as characters. **some people like to write stuff to text files, and read from text files without having to use the isstream class if your doing most of your work on a windows platform.
Last edited by Ancient Dragon; Jan 26th, 2008 at 9:33 pm. Reason: add code tags
Name: Cody Oebel
Place : San Antonio , TX.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
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: 1436
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: inputing a text file into an array

 
0
  #4
Jan 26th, 2008
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.
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
  #5
Jan 27th, 2008
Ok, sorry for not showing you what i've got.

I'm looking for something really simple, all I want to do is fill an array/matrix with my infile!!

In my example, there were only a few rows of information, but this needs to be able to be replaced with something, say, of 100 row of information. Thus, the program needs to be quite general.

I thought maybe zero the array first?

At the moment I have this code for the array...

k = 0;

for (i=1; i<101; i++)

{

for (j=1; j<3; j++)
{
matrix[i][0] = i;
matrix[i][1] = k;
matrix[i][2] = k; }

printf("%d", matrix[i][0]);
printf("\t%d", matrix[i][1],k);
printf("\t%d", matrix[i][2],k);
printf("\n");}

}






And as for uploading the input file from notepad, i believe i should use...

inFile.open("input.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}

Am I on the right tracks? Oh, and that code already posted din't work at all
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,756
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: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: inputing a text file into an array

 
0
  #6
Jan 27th, 2008
You don't need to zero out the array. You already know the size (5 x 3), so declare it that size:

  1. int matrix[5][3];


i would just read it in like this:

  1. for (int i = 0; i < 5; i++)
  2. {
  3. for (j = 0; j < 3; j++)
  4. {
  5. inFile >> matrix[i][j];
  6. // display matrix[i][j]
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: inputing a text file into an array

 
0
  #7
Jan 27th, 2008
Originally Posted by VernonDozier
You don't need to zero out the array. You already know the size (5 x 3), so declare it that size:
Nope...


Originally Posted by andyg55
In my example, there were only a few rows of information, but this needs to be able to be replaced with something, say, of 100 row of information. Thus, the program needs to be quite general.
*Voted best profile in the world*
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
  #8
Jan 27th, 2008
Yeah, sadly it needs to be replaced with a file of unknown size

I've been told it won't be over 100 rows though
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,756
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: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: inputing a text file into an array

 
0
  #9
Jan 27th, 2008
Missed that part of the problem. Thanks.
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
  #10
Jan 27th, 2008
So here is the basic program I have. All this does is read a .txt file and produce a 100x3 array that is zero'd. What alterations/additions do I make to the code to import the .txt file into the array? Thanks in advance!!
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4.  
  5. int k,i,j;
  6. int matrix[101][5];
  7.  
  8. main () {
  9.  
  10. ifstream inFile;
  11. inFile.open("input.txt");
  12.  
  13. if (!inFile) {
  14. cout << "Unable to open file";
  15. exit(1); // terminate with error
  16. }
  17. inFile.close();
  18.  
  19. k = 0;
  20.  
  21. for (i=1; i<101; i++)
  22.  
  23. {
  24.  
  25. for (j=1; j<3; j++)
  26. {
  27. matrix[i][0] = k;
  28. matrix[i][1] = k;
  29. matrix[i][2] = k; }
  30.  
  31. printf("%d", matrix[i][0]);
  32. printf("\t%d", matrix[i][1],k);
  33. printf("\t%d", matrix[i][2],k);
  34. printf("\n");}
  35.  
  36. }
Last edited by Ancient Dragon; Jan 27th, 2008 at 6:01 pm. Reason: add code tags
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