943,836 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5634
  • C++ RSS
Oct 8th, 2006
0

txt data into 2d Array

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Oct 8th, 2006
0

Re: txt data into 2d Array

That didn't work
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Oct 8th, 2006
0

Re: txt data into 2d Array

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
C++ Syntax (Toggle Plain Text)
  1. int wksales[rows];
  2.  
  3. infile.read ( &wksales [i], sizeof(int) )

if data.txt is a text file, then read it like this
C++ Syntax (Toggle Plain Text)
  1. int wksales[rows];
  2.  
  3. infile >> wksales[i];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 8th, 2006
0

Re: txt data into 2d Array

The file is in Notepad so I used that code.I also change the char array to int it's not working.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Oct 8th, 2006
0

Re: txt data into 2d Array

Click to Expand / Collapse  Quote originally posted by Sashar400 ...
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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 8th, 2006
0

Re: txt data into 2d Array

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Oct 8th, 2006
0

Re: txt data into 2d Array

In general, you have to read all three colums, and just igore the first 2.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Oct 8th, 2006
0

Re: txt data into 2d Array

>>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.

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Game Of Life
Next Thread in C++ Forum Timeline: Help please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC