Hi every one,
i got a trouble with opening and reading multi files (200 files) in. These files include 4 columns and 1010 rows. event i have tried many times but it still can not work. if possible, could you please give me some suggestion on my code as the following?

void snapshot::snapshot_mean(double U[Smax+3][Xmax][Ymax])
{
	int i,j,k,m;
	double sum = 0.0;
	char file_name[50]; 
	char file_list[10];

//	double U1[200][TSpcs+3][Xmax][Ymax];
	double ****U1;

// Set the temporary valuables

	U1 = new double *** [200];
	for(m=1;m<=200; m++) U1[m] = new double **[TSpcs+4];

	for(m=1;m<=200; m++){
		for(k=0;k<=TSpcs+3; k++) U1[m][k] = new double *[Xmax];
	}
	for(m=1;m<=200; m++){
		for(k=0;k<=TSpcs+3; k++) {
			for (i=0; i<Xmax; i++) U1[m][k][i]= new double [Ymax];
		}
	}

// Set the initial values of U_mean
		for(j=0; j<=d+8; j++){
			for(i=0; i<=l+8; i++) {
				for(k=1; k<=TSpcs+3; k++){
					U_mean[k][i][j] = 0.0;
				}
			}
		}

// open and read snapshots in, then compute sum of snapshots
	for(m=1;m<=200;m++){
		strcpy(file_name,"./outdata/conserv");
		sprintf(file_list, "%d", (int)m);
		strcat(file_name, file_list);
		strcat(file_name, ".dat");	
		ifstream fin(file_name,ios::in | ios::binary);
		if(!file_name) { 
			cerr << "Error: file could not be opened" << endl;
			exit(1);
		}
		for(j=0; j<=d+8; j++){
			for(i=0; i<=l+8; i++) {
				for(k=1; k<=TSpcs+3; k++){
					fin.read((char *) &U[k][i][j], sizeof(double));
					if(ABS(U[k][i][j]) <= 1.0e-20) U[k][i][j] = 0.0;
					U1[m][k][i][j] = U[k][i][j];
//					cout<<U[k][i][j]<<" \n";
				}
			}   
		}
		fin.close();
	}

// Compute mean 0f snapshots 
	for(m=1;m<=200;m++){
		for(j=0; j<=d+8; j++){
			for(i=0; i<=l+8; i++) {
				for(k=1; k<=TSpcs+3;k++){
					U_mean[k][i][j] = U_mean[k][i][j] + U1[m][k][i][j];
				}
			}   
		}
	}
	for(j=0; j<=d+8; j++){
		for(i=0; i<=l+8; i++) {
			for(k=1; k<=TSpcs+3;k++){
				U_mean[k][i][j] = U_mean[k][i][j]/8;
				cout<<U_mean[k][i][j]<<" \n";
			}
		}   
	}

// Store new snapshot
	for(m=1;m<=200;m++){
		strcpy(file_name,"./outdata/new_snapshot");
		sprintf(file_list, "%d", (int)m);
		strcat(file_name, file_list);
		strcat(file_name, ".dat");
		ofstream out(file_name);
		for(j=0; j<=d+8; j++) {
			for(i=0; i<=l+8; i++) {
				for(k=1; k<=TSpcs+3; k++) out<<(U1[m][k][i][j] - U_mean[k][i][j])<<" ";
//				for(k=1; k<=TSpcs+3; k++) out<<U_mean[k][i][j]<<" ";
				out<<"\n";
			}
		}
		out.close();
	}
}

Thank you very much in advance,
Bomtk.,

Recommended Answers

All 2 Replies

1. Use CODE tags for your code snippet:

[code=cplusplus] your code here

[/code]
2. Try to correct absolutely barbaric text indentation.

3. Invalid file opened check in the loop, must be:

if (!fin /*!file_name*/ ) { 
        cerr << "Error: file #" << m << " could not be opened" << endl;
        exit(1);
    }

No need in fin.close() at the end of a loop body. You declare fin in the body block so fin is a local for loop body variable, it created and destroyed (closed) on every loop circuit.

Try to correct this source and at first check if your files are opened.

i already fixed it,

Thanks so much for your guidance.

bomtk

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.