Write a matrix to a txt file
-
I want to insert the values i get from a matrix, in the order thay are in a text file. I mean by this the values to keep their place, on lines and columns. To read the values I did this: double LocalMap::SquareValue(int i, int j) { for (i=0; i0){ cellsV[i][j]=cellV[i][j]*cellV[i][j]; if (cellsV[i][j]>noise_border){ ofstream BuiltMap; //mode écriture BuiltMap.open ("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in); //open file while (!BuiltMap.eof()){ //laisser i colonnes libres et j espaces libres BuiltMap<<"\n"<<" "<
-
I want to insert the values i get from a matrix, in the order thay are in a text file. I mean by this the values to keep their place, on lines and columns. To read the values I did this: double LocalMap::SquareValue(int i, int j) { for (i=0; i0){ cellsV[i][j]=cellV[i][j]*cellV[i][j]; if (cellsV[i][j]>noise_border){ ofstream BuiltMap; //mode écriture BuiltMap.open ("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in); //open file while (!BuiltMap.eof()){ //laisser i colonnes libres et j espaces libres BuiltMap<<"\n"<<" "<
for (i = 0; i < nx; i++)
{
for (j = 0; j < ny; j++)
{
if (cellV[i][j] > 0)
{
cellsV[i][j] = cellV[i][j] * cellV[i][j];
if (cellsV[i][j] > noise_border)
{
ofstream BuiltMap;
BuiltMap.open("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in);
while (! BuiltMap.eof())
{
BuiltMap << "\n" << " " << cellsV[i][j];
}
}
else
cellsV[i][j] = NULL;
}
}
}Aside from the fact that the opening/closing of the file should be done outside of the loops, I'm not quite sure what you are doing here. You've got an
ofstream
object that is opening a file for input (i.e., reading), yet you are usign the << operator to write to it. Is this intentional?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
for (i = 0; i < nx; i++)
{
for (j = 0; j < ny; j++)
{
if (cellV[i][j] > 0)
{
cellsV[i][j] = cellV[i][j] * cellV[i][j];
if (cellsV[i][j] > noise_border)
{
ofstream BuiltMap;
BuiltMap.open("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in);
while (! BuiltMap.eof())
{
BuiltMap << "\n" << " " << cellsV[i][j];
}
}
else
cellsV[i][j] = NULL;
}
}
}Aside from the fact that the opening/closing of the file should be done outside of the loops, I'm not quite sure what you are doing here. You've got an
ofstream
object that is opening a file for input (i.e., reading), yet you are usign the << operator to write to it. Is this intentional?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
I thought ifstream is made for reading files only, and ofstream to be able to write into file. Did I miss something there? I want to write the values cellsV[i][j] into the BuiltMap file. THX
So shouldn't you be opening the file with
ios::out
instead?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)