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.

for(int i = 0; i <rows; i++) 
 
{
inFile.read(wksales[i], columns);
}
 
for( i = 0; i < 4;i++)
 
{ 
 
for(int j = 0; j <5;j++)
 
 
{
printf("%2d ", wksales[i][j]);  //EXAMPLE from book
 
} 
 
printf("\n");
}

#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++)

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

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

{

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


{
printf("%2d ", 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;

}

That didn't work

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

int wksales[rows];

infile.read ( &wksales [i], sizeof(int) )

if data.txt is a text file, then read it like this

int wksales[rows];

infile >> wksales[i];

The file is in Notepad so I used that code.I also change the char array to int it's not working.

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 :)

#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;
}

In general, you have to read all three colums, and just igore the first 2.

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

//for( i = 0; i <rows; i++) 
{
int wksales[rows];
 
inFile >> wksales[i];
 
}

above is useless crap. you may as well delete it.

int wkdays[rows];

// read all the rows, ignore the first two integers
int i = 0;
int a,b,c;
while( i < rows && InFile >> a >> b >> c)
{
    wkdays[i] = c;
    ++i;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.