473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

reading a text file into a 2d array problem

Hello.
I am trying to read a text file that contains

1
2
3

it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix[i][j] = {a,b,c};

At some point during tweaking all the matrix elements became 0 and I have lost myself.
Look at my code please.
I realise this is not the best programming way of writing code but I'm a scientist and I'd rather do it the way I have. If its too annoying to read then please suggest something else.



My code below.

Expand|Select|Wrap|Line Numbers
  1. #include<sstream>
  2. #include <iostream>
  3. #include<fstream>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8. int i,j;
  9. int a,b,c;
  10.  
  11.  
  12. int main ()
  13. {
  14.  // getting the data from a text file
  15.         string str;
  16.   ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
  17.   // above opens the file
  18.   if (myfile.is_open()) // if open do the following
  19.   {
  20.     while (! myfile.eof() ) // while loop to do the below until the end of the file
  21.         {
  22.                   getline(myfile, str);
  23.                   cout<<str<<endl;
  24.           // matrix defining
  25.                   int matrix[3][1]={i,j};
  26.           for (i=0; i<3; i++)
  27.           for (j=0; j<1; j++)
  28.  
  29.  
  30.           myfile >> matrix[0][0]  >>  matrix[1][0] >>  matrix[2][0] >> ws;
  31.  
  32.           matrix[0][0] = a;
  33.           matrix[1][0] = b;
  34.           matrix[2][0] = c;
  35.  
  36.          int matrix[i][j] = {a, b, c};         // error here C2057 & C2087, due to the a=b=c=0 at the moment
  37.  
  38. // assigned the 3 numbers to the matrix elements
  39. cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
  40.  
  41.         //cout << matrix[i][j];
  42.  
  43. // attempted string to int conversion
  44. //p = atof(line );
  45.  
  46.  
  47.         }
  48.  
  49. myfile.close();
  50.   }
  51.  
  52.  
  53.  else cout << "Unable to open file";
  54.  
  55.   return 0;
  56. }
thanks
Ben
May 25 '07 #1
10 7469
Ganon11
3,652 Expert 2GB
Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values.

What exactly are you trying to do? I imagine there's a much simpler, cleaner option than all this.
May 25 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I think you are making this too hard.

First, there really aren't 2D arrays in C++. There are only one-dimensional arrays. That is:
Expand|Select|Wrap|Line Numbers
  1. int matrix[6] = {1,2,3,4,5,6};
  2.  
is laid out in memory as 1 2 3 4 5 6.

Whereas this array:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][2] = {1,2,3,4,5,6};
  2.  
is laid out in memory as 1 2 3 4 5 6.

Kinda the same, right?

The "dimensions" are really descriptions of the elements. In the first example there are 6 elements in the matrix array and each element is an int. In the second example, there are 3 elements in the array and each element is an array of 2 int.

You use the "dimensions" as a convenience for doing math.

However, since all arrays are laid out serially in memory, I suggest you write your 2D array out as individual integers starting with &arr[0][0]. For example, an int arr[10][4] has 40 integers. Start with &arr[0][0] and write out 40 ints.

On the restore, start at &arr[0][0] and read 40 ints.

Once that's done you can use your array as a 2D array as you normally would.
May 25 '07 #3
Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values.

What exactly are you trying to do? I imagine there's a much simpler, cleaner option than all this.

I'm trying to read values from a text file and make a "matrix". As c++ doesnt understand what a matrix is without a "matrix header file" i'm using arrays. I wanted to be able to call matrix[i][j] and the output should be 1,2,3 which was read from the text file.
This matrix will be used in matrix equation algebra. The matrix represents a 3d point and I'm trying to translate and rotate this 3d point to match up with another 3d point. I have all the maths equations its just trying to implement them in c++ that's the dificulty. I'm at home and can't tell you exactly what maths will be used on the matrix. Does that answer your question?


[quote=Ganon11] after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively

I was trying to read the text file values into each element of the matrix, when this didnt work I thought it was due a string to int problem. I thought that by assigning matrix[0][0] to an int a it would change the string to an integer. Basically I'm unsure if reading from the text file gives me a string or because the file only contains number if it will be an int. I see now what I did is problematic but I don't see how to do what I want.

also how do initialize a,b,c? I said they were int at the beginning of the code but you mean initialize a value? I have seen people do a=b=c=0 before using them but does that then not make them 0 and I wanted them to be values from the file.
May 27 '07 #4
weaknessforcats I have made it too dificult but I really need to stick to the matrix[i][j] format. This is so I understand what I'm doing in terms of matrices or else I may get confused. I could individually assign elements like matrix[0][0] which is what I was doing but I need to be able to get an output for matrix[i][j] which should read 1,2,3 So I can use it in calculations later.

Thanks for the talk on arrays but I don't think what you suggested will help, my fault for not specifying what I wanted to do or aim to get.
May 27 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Yes, you have made it too difficult. However, at the risk of confusing you, I took your code and twiddled it to illustrate what I said:

Expand|Select|Wrap|Line Numbers
  1. #include<sstream>
  2. #include <iostream>
  3. #include<fstream>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8. int main ()
  9. {
  10.     //
  11.     //The target matrix
  12.    //
  13.     int matrix[3][1];
  14.     int* input = &matrix[0][0];
  15.  // getting the data from a text file
  16.         string str;
  17.   //ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
  18.   ifstream myfile("C:\\scratch\\instructor\\Data set 1.txt", ios::in);
  19.   // above opens the file
  20.   if (myfile.is_open()) // if open do the following
  21.   {
  22.     while (! myfile.eof() ) // while loop to do the below until the end of the file
  23.         {
  24.           myfile >> *input;  //put the int in the matrix array
  25.           ++input;               //set up for the next element
  26.  
  27.         }
  28.  
  29.  
  30.   }
  31.   else
  32.   {
  33.       cout << "Unable to open file";
  34.   }
  35.  
  36.   // assigned the 3 numbers to the matrix elements
  37.   cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
  38.  
  39.   return 0;
  40. }
  41.  
First, I declared your matrix at the beginning of main()
Second, I declared an int* to contain the address of matrix[0][0]
Third, I de-reference the the int* when I use >>
Fourth, I increment the int* after using it to set up for the next element
Fifth, At the end of main(), I display the 2D matrix and there are your integers.
May 27 '07 #6
First, I declared your matrix at the beginning of main()
Second, I declared an int* to contain the address of matrix[0][0]
Third, I de-reference the the int* when I use >>
Fourth, I increment the int* after using it to set up for the next element
Fifth, At the end of main(), I display the 2D matrix and there are your integers.[/quote]


I understand now what you meant and see how yours works and mine didn't. I do have one question though.
Why can I not output matrix[3][1] and it give me 1,2,3? Do I always have to work with individual elements of the array?

I will be doing some addition, multiplication etc and was hoping not to have to do it using individual elements. If thats the only way to do it then obviously I will, its just later I intend on taking a set of matrice's and it would get tedious doing say 10 matrices.
Thanks for the help you guys I shall press on and if I get stuck I know where to come.
May 28 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Why can I not output matrix[3][1] and it give me 1,2,3? Do I always have to work with individual elements of the array?
Your example is matrix[3][1]. That is 3 arrays with elements that are arrays of 1 int each.

So, matrix[0] is the element 0 in matrix. and matrix[0][1] is element 0 in
matrix[0].

This is identical to matrix[3]. matrix[3] is an array of 3 int.

You will work with individual elements when you work with the ints themselves.
May 28 '07 #8
Your example is matrix[3][1]. That is 3 arrays with elements that are arrays of 1 int each.

So, matrix[0] is the element 0 in matrix. and matrix[0][1] is element 0 in
matrix[0].

This is identical to matrix[3]. matrix[3] is an array of 3 int.

You will work with individual elements when you work with the ints themselves.


From what you mentioned, is it possible to have matrix[3][1] give 1,2,3 if i didn't assign 1,2 or 3 as int's.
Sorry if this is a dumb question.
May 30 '07 #9
Another question related to my code.

I have moved on and am now trying to implement an svd algirithm to my code.

My huge code is below,
#include<sstream>
#include <iostream>
#include<fstream>
#include <string>
#include <stdlib.h>
#include <math.h>
#include "nrutil.h"

using namespace std;


int main ()
{
//
// matrix a definition
//
int amatrix[3][1];
int* inputa = &amatrix[0][0];
// getting the data from a text file
string stra;
ifstream myfilea("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);

// above opens the file
if (myfilea.is_open()) // if open do the following
{
while (! myfilea.eof() ) // while loop to do the below until the end of the file
{
myfilea >> *inputa; //put the int in the matrix array
++inputa; //set up for the next element

}


}
else
{
cout << "Unable to open file";
}

// assigned the 3 numbers to the matrix elements
//cout << amatrix[0][0] << " " << amatrix[1][0] << " " << amatrix[2][0] << endl;


//
//matrixb definition
//
int bmatrix[3][1];
int* inputb = &bmatrix[0][0];
// getting the data from a text file
string strb;
ifstream myfileb("C:\\brooks\\C++ examples\\Data set 2.txt", ios::in);

// above opens the file
if (myfileb.is_open()) // if open do the following
{
while (! myfileb.eof() ) // while loop to do the below until the end of the file
{
myfileb >> *inputb; //put the int in the matrix array
++inputb; //set up for the next element

}


}
else
{
cout << "Unable to open file";
}

// assigned the 3 numbers to the matrix elements, this is my b matrix
//cout << bmatrix[0][0] << " " << bmatrix[1][0] << " " << bmatrix[2][0] << endl;

//matrix a is pi.
// for this example pi = p due to only using one matrix per data set.

// matrix b is p'i.
// for this example p'i=p' due to only using one matrix per data set.

// from Haung and Blostein's paper, " least squares fitting of Two 3-d point sets
// qi = pi - p, q'i = p'i - p'.


// qi matrix definition
int qmatrix[3][1];
int* inputq = &qmatrix[0][0];

// q'i matrix definition
int qamatrix[3][1];
int* inputqa = &qamatrix[0][0];

// qi = matrixa - matrixb, q'i = matrixa - matrixb.
// have to do them as seperate elements
// qi matrix below

qmatrix[0][0] = amatrix[0][0] - bmatrix[0][0];
qmatrix[1][0] = amatrix[1][0] - bmatrix[1][0];
qmatrix[2][0] = amatrix[2][0] - bmatrix[2][0];

//cout<< qmatrix[0][0] << qmatrix[1][0] <<qmatrix[2][0];

//q'i matrix below
qamatrix[0][0] = amatrix[0][0] - bmatrix[0][0];
qamatrix[1][0] = amatrix[1][0] - bmatrix[1][0];
qamatrix[2][0] = amatrix[2][0] - bmatrix[2][0];

cout<< qamatrix[0][0]<<" "<< qamatrix[1][0]<<" "<<qamatrix[2][0]<< "\n";


// H is defined by equation 11, sum of (qi q'i).
// H is a 3 by 3 matrix.

int Hmatrix[3][3];
int* inputH = &Hmatrix[0][0];

Hmatrix[0][0]= qmatrix[0][0]*qamatrix[0][0];
Hmatrix[0][1]= qmatrix[0][0]*qamatrix[1][0];
Hmatrix[0][2]= qmatrix[0][0]*qamatrix[2][0];
Hmatrix[1][0]= qmatrix[1][0]*qamatrix[0][0];
Hmatrix[1][1]= qmatrix[1][0]*qamatrix[1][0];
Hmatrix[1][2]= qmatrix[1][0]*qamatrix[2][0];
Hmatrix[2][0]= qmatrix[2][0]*qamatrix[0][0];
Hmatrix[2][1]= qmatrix[2][0]*qamatrix[1][0];
Hmatrix[2][2]= qmatrix[2][0]*qamatrix[2][0];

cout<< Hmatrix[0][0]<< " "<< Hmatrix[0][1]<<" "<< Hmatrix[0][2]<<" "<< Hmatrix[1][0]<<" "<< Hmatrix[1][1]<<" "<< Hmatrix[1][2]<<" "<< Hmatrix[2][0]<<" "<< Hmatrix[2][1]<<" "<< Hmatrix[2][2];



////////////////////////////////////////// SVD of a NxM Matrix //////////////////////////////////

int n;
int m;
float **v;
float w[3]; // for our case n is 3.
float **a;

void svdcmp(**a, m,n, w[3], **v);
/*Given a matrix a[1..m][1..n], this routine computes its singular value decomposition, A =
U•W•V T. Thematrix U replaces a on output. The diagonal matrix of singular values W is output
as a vector w[1..n]. Thematrix V (not the transpose V T ) is output as v[1..n][1..n].*/
{
Hmatrix[3][3] = a[m][n]; // i added this to make Hmatrix be the same as below
float pythag(float a,float b);
int flag,i,its,j,jj,k,l,nm;
float anorm,c,f,g,h,s,scale,x,y,z,*rv1;
rv1=vector(1,n);
g=scale=anorm=0.0; //Householder reduction to bidiagonal form.
for (i=1;i<=n;i++) {
l=i+1;
rv1[i]=scale*g;
g=s=scale=0.0;
if (i <= m) {
for (k=i;k<=m;k++) scale += fabs(a[k][i]);
if (scale) {
for (k=i;k<=m;k++) {
a[k][i] /= scale;
s += a[k][i]*a[k][i];
}
f=a[i][i];
g = -SIGN(sqrt(s),f);
h=f*g-s;
a[i][i]=f-g;
for (j=l;j<=n;j++) {
for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
f=s/h;
for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
}
for (k=i;k<=m;k++) a[k][i] *= scale;
}
}
w[i]=scale *g;
g=s=scale=0.0;
if (i <= m && i != n) {
for (k=l;k<=n;k++) scale += fabs(a[i][k]);
if (scale) {
for (k=l;k<=n;k++) {
a[i][k] /= scale;
s += a[i][k]*a[i][k];
}
f=a[i][l];
g = -SIGN(sqrt(s),f);
h=f*g-s;
a[i][l]=f-g;
for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
for (j=l;j<=m;j++) {
for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
}
for (k=l;k<=n;k++) a[i][k] *= scale;
}
}
anorm=FMAX(anorm,(fabs(w[i])+fabs(rv1[i])));
}
for (i=n;i>=1;i--) { //Accumulation of right-hand transformations.
if (i < n) {
if (g) {
for (j=l;j<=n;j++) //Double division to avoid possible underflow.
v[j][i]=(a[i][j]/a[i][l])/g;
for (j=l;j<=n;j++) {
for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j];
for (k=l;k<=n;k++) v[k][j] += s*v[k][i];
}
}
for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0;
}
v[i][i]=1.0;
g=rv1[i];
l=i;
}
for (i=IMIN(m,n);i>=1;i--) { //Accumulation of left-hand transformations.
l=i+1;
g=w[i];
for (j=l;j<=n;j++) a[i][j]=0.0;
if (g) {
g=1.0/g;
for (j=l;j<=n;j++) {
for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
f=(s/a[i][i])*g;
for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
}
for (j=i;j<=m;j++) a[j][i] *= g;
} else for (j=i;j<=m;j++) a[j][i]=0.0;
++a[i][i];
}
for (k=n;k>=1;k--) { //Diagonalization of the bidiagonal form: Loop over singular values,
for (its=1;its<=30;its++) { //and over allowed iterations.
flag=1;
for (l=k;l>=1;l--) { //Test for splitting.
nm=l-1; //Note that rv1[1] is always zero.*/
if ((float)(fabs(rv1[l])+anorm) == anorm) {
flag=0;
break;
}
if ((float)(fabs(w[nm])+anorm) == anorm) break;
}
if (flag) {
c=0.0; //Cancellation of rv1[l], if l > 1.
s=1.0;
for (i=l;i<=k;i++) {
f=s*rv1[i];
rv1[i]=c*rv1[i];
if ((float)(fabs(f)+anorm) == anorm) break;
g=w[i];
h=pythag(f,g);
w[i]=h;
h=1.0/h;
c=g*h;
s = -f*h;
for (j=1;j<=m;j++) {
y=a[j][nm];
z=a[j][i];
a[j][nm]=y*c+z*s;
a[j][i]=z*c-y*s;
}
}
}
z=w[k];
if (l == k) { //Convergence.
if (z < 0.0) { //Singular value is made nonnegative.
w[k] = -z;
for (j=1;j<=n;j++) v[j][k] = -v[j][k];
}
break;
}
if (its == 30) nrerror("no convergence in 30 svdcmp iterations");
x=w[l]; //Shift from bottom 2-by-2 minor.
nm=k-1;
y=w[nm];
g=rv1[nm];
h=rv1[k];
f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
g=pythag(f,1.0);
f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
c=s=1.0; //Next QR transformation:
for (j=l;j<=nm;j++) {
i=j+1;
g=rv1[i];
y=w[i];
h=s*g;
g=c*g;
z=pythag(f,h);
rv1[j]=z;
c=f/z;
s=h/z;
f=x*c+g*s;
g = g*c-x*s;
h=y*s;
y *= c;
for (jj=1;jj<=n;jj++) {
x=v[jj][j];
z=v[jj][i];
v[jj][j]=x*c+z*s;
v[jj][i]=z*c-x*s;
}
z=pythag(f,h);
w[j]=z; //Rotation can be arbitrary if z = 0.
if (z) {
z=1.0/z;
c=f*z;
s=h*z;
}
f=c*g+s*y;
x=c*y-s*g;
for (jj=1;jj<=m;jj++) {
y=a[jj][j];
z=a[jj][i];
a[jj][j]=y*c+z*s;
a[jj][i]=z*c-y*s;
}
}
rv1[l]=0.0;
rv1[k]=f;
w[k]=x;
}
}
free_vector(rv1,1,n);
}
float pythag(float a, float b)
//Computes (a2 + b2)1/2 without destructive underflow or overflow.
{
float absa,absb;
absa=fabs(a);
absb=fabs(b);
if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
}

////////////////////////////////////////////////////////////////////////////////////////////////

return 0;
}


I get errors in the svd part of the code which I'm trying to iron out.

c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : error C2182: 'svdcmp' : illegal use of type 'void'
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : error C2078: too many initializers
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(142) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(215) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(246) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(271) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(298) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(320) : error C2601: 'pythag' : local function definitions are illegal
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(13): this line contains a '{' which has not yet been matched
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(324) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(325) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data

My issue is trying to make my Hmatrix be the a[n][m] matrix. One of the errors I was getting said that the Hmatrix was undefined, this occured when I put it in the svdcmp function but clearly I have defined it above.

So my question is how do I use my matrix in that svd algorithm. The algorithm was taken from numeric recipes in c, written by Press amognst others.

Thanks
May 30 '07 #10
Hi
This was a nice code.
I am doing somewhat similar code.
Can u help me for the following code.
I want to read a text file containing 2 d array .
Then I want to check the 1 st column and if condition is satisfied I want to use the values in that column and corresponding in 2nd column.
At the end I want to store again a 2D array in another text file.
Will you help me with some sample code.

You can directly mail me at dbhushan@gmail.com
Thanks!
Regards
Bhushan

Hello.
I am trying to read a text file that contains

1
2
3

it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix[i][j] = {a,b,c};

At some point during tweaking all the matrix elements became 0 and I have lost myself.
Look at my code please.
I realise this is not the best programming way of writing code but I'm a scientist and I'd rather do it the way I have. If its too annoying to read then please suggest something else.



My code below.

Expand|Select|Wrap|Line Numbers
  1. #include<sstream>
  2. #include <iostream>
  3. #include<fstream>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8. int i,j;
  9. int a,b,c;
  10.  
  11.  
  12. int main ()
  13. {
  14.  // getting the data from a text file
  15.         string str;
  16.   ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
  17.   // above opens the file
  18.   if (myfile.is_open()) // if open do the following
  19.   {
  20.     while (! myfile.eof() ) // while loop to do the below until the end of the file
  21.         {
  22.                   getline(myfile, str);
  23.                   cout<<str<<endl;
  24.           // matrix defining
  25.                   int matrix[3][1]={i,j};
  26.           for (i=0; i<3; i++)
  27.           for (j=0; j<1; j++)
  28.  
  29.  
  30.           myfile >> matrix[0][0]  >>  matrix[1][0] >>  matrix[2][0] >> ws;
  31.  
  32.           matrix[0][0] = a;
  33.           matrix[1][0] = b;
  34.           matrix[2][0] = c;
  35.  
  36.          int matrix[i][j] = {a, b, c};         // error here C2057 & C2087, due to the a=b=c=0 at the moment
  37.  
  38. // assigned the 3 numbers to the matrix elements
  39. cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
  40.  
  41.         //cout << matrix[i][j];
  42.  
  43. // attempted string to int conversion
  44. //p = atof(line );
  45.  
  46.  
  47.         }
  48.  
  49. myfile.close();
  50.   }
  51.  
  52.  
  53.  else cout << "Unable to open file";
  54.  
  55.   return 0;
  56. }
thanks
Ben
Jun 15 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
1
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read...
3
by: lizii | last post by:
i have a file - which on each line has some data i need to fill into a box - now although reading in the data is simple enough and putting it in the correct box will be no problem, as i can just...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.