txt data into 2d Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

txt data into 2d Array

 
0
  #1
Oct 8th, 2006
Hi I am new to this forum and to c++.I am having problems reading data from a txt file into a 2d array. The data is in the file like this

1 5 87
3 3 24
2 4 62
4 2 22

I am only suppose to read in the 3rd column into the array. I am opening the file but the data that I am seeing is not the data in the file.but all calculations on these columns are correct. I don't know how to do the 3rd column part .Here is the code that is giving me problems . Please help.I really need help in getting the data from the file into the 2D array.

  1. for(int i = 0; i <rows; i++)
  2.  
  3. {
  4. inFile.read(wksales[i], columns);
  5. }
  6.  
  7. for( i = 0; i < 4;i++)
  8.  
  9. {
  10.  
  11. for(int j = 0; j <5;j++)
  12.  
  13.  
  14. {
  15. printf("%2d ", wksales[i][j]); //EXAMPLE from book
  16.  
  17. }
  18.  
  19. printf("\n");
  20. }




#include <iostream>
#include <fstream>
usingnamespace std;

int main()

{


const int rows=4;
const int columns=5;
char wksales[rows][columns];

ifstream inFile;

inFile.open("c:/data.txt",ios::in);

if (!inFile)
{
cout << "Unable to open file";
exit(1);
}



int j,i;
for( i = 0; i <rows; i++)

{
inFile.read(wksales[i][j]);
}

for( i = 0; i < 4;i++)

{

for( j = 0; j <5;j++)


{
printf("%2d ", wksales[i][j]);


}


printf("\n");
}




//sum of each individual column
for (int col=0;col<columns ;col++)

{
int totals=0;

for(int row=0;row<rows ;row++)

totals= totals +wksales[row][col];

cout<<" "<<totals<<" ";
}


inFile.close();
return 0;

}
Last edited by Sashar400; Oct 8th, 2006 at 4:09 pm. Reason: Please use code tags for displaying your code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

Re: txt data into 2d Array

 
0
  #2
Oct 8th, 2006
That didn't work
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,366
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: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: txt data into 2d Array

 
0
  #3
Oct 8th, 2006
1. is the input file (data.txt) a binary file or a text file? If you can read it with Notepad.exe then its a text file, otherwise if the data is unreadable then its a binary file. Only binary files are read with iostream.read() function, text files use getline() or the imput operator >>.

2. wdsales() is a 2d character array. If data.txt is a binary file then you need an integer array
  1. int wksales[rows];
  2.  
  3. infile.read ( &wksales [i], sizeof(int) )

if data.txt is a text file, then read it like this
  1. int wksales[rows];
  2.  
  3. infile >> wksales[i];
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

Re: txt data into 2d Array

 
0
  #4
Oct 8th, 2006
The file is in Notepad so I used that code.I also change the char array to int it's not working.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,366
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: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: txt data into 2d Array

 
0
  #5
Oct 8th, 2006
Originally Posted by Sashar400 View Post
The file is in Notepad so I used that code.I also change the char array to int it's not working.
Post your latest code -- we are not mind readers and I don't have my crystle ball with me today
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

Re: txt data into 2d Array

 
0
  #6
Oct 8th, 2006
  1.  
  2. #include
<iostream>
#include <fstream>
using namespace std;
 
int main()
{
const int rows=4;
const int columns=5;
char wksales[rows][columns];
 
ifstream inFile;

inFile.open("c:/data.txt",ios::in);
 
if (!inFile) 
{
cout << "Unable to open file";
exit(1); 
}

int j,i;

//for( i = 0; i <rows; i++) 
{
int wksales[rows];
 
inFile >> wksales[i];
 
}
 
{ 
{
for(i=0; i<rows; i++) //show values read from file
cout << wksales[i] << " ";
for(j=0; j<5; j++) 
cout << wksales[j] << " ";
} 
//printf("\n");
}
//sum of each individual column
for (int col=0;col<columns ;col++)
 
{
int totals=0;
for(int row=0;row<rows ;row++)
 
totals= totals +wksales[row][col];
cout<<" "<<totals<<" ";
}
  
inFile.close();
return 0;
}
Last edited by ~s.o.s~; Oct 8th, 2006 at 5:22 pm. Reason: Learn to use code tags, i wont do it everytime.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: txt data into 2d Array

 
0
  #7
Oct 8th, 2006
In general, you have to read all three colums, and just igore the first 2.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,366
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: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: txt data into 2d Array

 
0
  #8
Oct 8th, 2006
>>char wksales[rows][columns];

you did not change this like I suggested. wksales can NOT be a 2d array of characters. The instructions say you need a 1d array of ints.

  1. //for( i = 0; i <rows; i++)
  2. {
  3. int wksales[rows];
  4.  
  5. inFile >> wksales[i];
  6.  
  7. }
above is useless crap. you may as well delete it.

  1. int wkdays[rows];
  2.  
  3. // read all the rows, ignore the first two integers
  4. int i = 0;
  5. int a,b,c;
  6. while( i < rows && InFile >> a >> b >> c)
  7. {
  8. wkdays[i] = c;
  9. ++i;
  10. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC