Con la speranza che qualcuno mi risponda...
Ho praticamente terminato il codice, c'e' solo un'imperfezione: come ultimo valore, anziche' leggere la coordinata y corrispondente all'ultimo nodo, mi restituisce automaticamente uno 0.
Qualcuno riesce a capire a cosa e' dovuto?
codice:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype> // gestione dei caratteri isalnum()
using namespace std;
int node_num; // number of nodes
float *cord_x = NULL;
float *cord_y = NULL;
void ReadMatrix( string FileIn )
{
// open file
ifstream in( FileIn.c_str() );
if( !in )
{
cerr << "Could not open file: " << FileIn << endl;
exit( 1 );
}
// read the number of nodes
string line;
stringstream ss;
string token;
while( getline(in, line) ) {
ss << line;
}
while( ss >> token ) {
if( token.find("DIMENSION") != string::npos ) {
ss >> node_num;
cout << "node num:" << node_num << endl;
}
if( token.find("NODE_COORD_SECTION") != string::npos ){
break;
}
}
cord_x = (float*) malloc ( node_num * sizeof(float) );
cord_y = (float*) malloc ( node_num * sizeof(float) );
int index;
float x, y;
// read the coordinates
for( int i =0; i<node_num; i++ ) {
ss >> index >> x >> y;
cord_x[index-1] = x;
cord_y[index-1] = y;
// cout << index << " " << x << " " << y << endl;
/* in questo punto, se stampo l'operazione appena fatta, mi
* restituisce correttamente tutti i nodi con le relative coordinate.
* Per quanto riguarda l'ultimo nodo invece, il valore relativo a
* cord_x e' corretto, mentre il valore relativo a cord_y (ultimo
* valore prima di EOF) e' inspiegabilmente 0.
*/
}
in.close();
}
void WriteMatrix( string FileOut)
{
ofstream out( FileOut.c_str() );
if( !out )
{
cerr << "Could not open file: " << FileOut << endl;
exit( 1 );
}
//out << "NODE \tCORD_x \t CORD_Y\t\n";
for(int i=0; i<node_num; i++)
{
out << (i+1) << " \t" << cord_x[i] << " \t" << cord_y[i] << endl;
}
out.close();
}
int main( int argc, char *argv[]) {
string FileIn, FileOut;
cout << "File name: " ; // per immettere il nome
cin >> FileIn; // del file da aprire
cout << endl; // direttamente da tastiera
/*if ( argc < 2 ) {
FileIn = "TESTIN";
FileOut = "TESTOUT";
}
else if ( argc < 3 ) {
*/
FileOut = FileIn;
FileOut.erase( FileOut.size() - 4, FileOut.size() );
FileOut += ".OUT";
// }
ReadMatrix(FileIn);
WriteMatrix(FileOut);
free(cord_x);
free(cord_y);
// delete(cord_x);
// delete(cord_y);
}