Problem with reading in

When I read in my file it takes the last number of the file and overwrites all my other numbers. Is this the file's fault or am I doing something wrong in the program?

A sample input file would be
3 3
1 2 3
4 5 6
7 8 9

When I run the program it first fills correctly, but then it runs through the fill loop again and fills it with all 9s.

[code]
#include
#include
using namespace std;

void mypause()
{
cout << "Press the Enter key to continue...";
cin.ignore();
cin.get();
}

void readin()
{
int C = 1; // Column
int R = 1; // Row
int n = 0;
int i = 0;
int j = 0;

ifstream A;
A.open ("A.dat");

A >> R >> C;

int** arr = new int*[R];
for(int iter = 0; iter != R; ++iter)
{
arr[iter] = new int[C];
}

while(!A.eof())
{
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
A >> n;
arr[i][j] = n;
//cout << n << i << j << endl;
}
}
//cout << "A done" << endl;

}
A.close();


// readin B
int C2 = 1; // Column
int R2 = 1; // Row
int n2 = 0;
int i2 = 0;
int j2 = 0;

ifstream B;
B.open ("B.dat");

B >> R2 >> C2;

int** arr2 = new int*[R2];
for(int iter2 = 0; iter2 != R2; ++iter2)
{
arr2[iter2] = new int[C2];
}



while(!B.eof())
{
for(i2 = 0; i2 < R2; i2++)
{
for(j2 = 0; j2 < C2; j2++)
{
B >> n2;
arr2[i2][j2] = n2;
//cout << n2 << i2 << j2 << endl;
}
}
//cout << "B done" << endl;

}
B.close();

if(R != R2 || C != C2)
{
cout << "The matrices' size does not match." << endl;
mypause();
exit(1);

}


// process A and B
int** addarr = new int*[R];
for(int iter = 0; iter != R; ++iter)
{
addarr[iter] = new int[C];
}

ofstream out_file;
out_file.open("C.txt");
if(!out_file) // checks if file can be opened //if file fails to open tells user then exits
{
cout << "Error: file could not be opened" << endl;
exit(1); //exit out of the program
}
for(i = 0; i < R; i++)
{
for(j = 0 ; j < C; j++)
{
addarr[i][j] = arr[i][j] + arr2[i][j];
// cout << addarr[i][j] << endl;
out_file << addarr[i][j] << " ";
}

out_file <<endl;
}
out_file.close();

cout << "Writing to file complete" << endl;


for(int iter = 0; iter != R; ++iter)
{
delete [] arr[iter];
}
delete [] arr;

for(int iter2 = 0; iter2 != R2; ++iter2)
{
delete [] arr2[iter2];
}
delete [] arr2;

for(int iter3 = 0; iter3 != R; ++iter3)
{
delete [] addarr[iter3];
}
delete [] addarr;

mypause();
}

int main()
{
readin();
}
[/code]

Comments

  • The problem is in

    [code]while(!A.eof())
    {
    for(i = 0; i < R; i++)
    {
    for(j = 0; j < C; j++)
    {
    A >> n;
    arr[i][j] = n;
    //cout << n << i << j << endl;
    }
    }
    //cout << "A done" << endl;

    }
    A.close();
    [/code]

    The eof flag is not set until you try to read past the end of the file.
    The first time the loop executes you're reading just as many integers as are present in the file, but are not trying to read past the end. So the eof check fails and the while loop is executed again, causing the eof flag to be set after the first read attempt. But the two for loops have already started running and they are assigning the value still stored in n (the last integer in the file) to each integer in the array.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion