| | |
txt data into 2d Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2006
Posts: 9
Reputation:
Solved Threads: 0
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.
#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;
}
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)
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>
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.
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
if data.txt is a text file, then read it like this
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)
int wksales[rows]; infile.read ( &wksales [i], sizeof(int) )
if data.txt is a text file, then read it like this
C++ Syntax (Toggle Plain Text)
int wksales[rows]; infile >> wksales[i];
•
•
Join Date: Aug 2006
Posts: 9
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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.
>>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.
above is useless crap. you may as well delete it.
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)
//for( i = 0; i <rows; i++) { int wksales[rows]; inFile >> wksales[i]; }
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Data consolidation across an array a.k.a. Hashed to death... heeelp... (Perl)
- array / data comparision help (C)
- Array data not setting (C++)
- input data from a file into an Array (C)
- adding data into an char array (C++)
- How can I read from a txt file? (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
Other Threads in the C++ Forum
- Previous Thread: Game Of Life
- Next Thread: Help please
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






