⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 load_save_file_data.cpp

📁 2维时域有限差分模拟
💻 CPP
字号:
#include "stdafx.h"
//#include "Load_Save_File_Data.h"
//#include "Matrix.h"
//#include <string.h>
//#include <cctype>

///////////////////////////////////////////////////////////////////////////////////////
//Save 1D vectors with double elements
///////////////////////////////////////////////////////////////////////////////////////
bool save_1D(double *Tomb, int n_x_a, int n_x_b, int n_t, char *ut_nev)
{
	int i;

	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	FILE *fp;
	
	strcat(File_Name,ut_nev);
	
	char buffer[256];
	sprintf(buffer, "_%i", n_t);
	strcat(File_Name,buffer);

	strcat(File_Name,".dat");

	fp = fopen(File_Name, "w+");
	if (!fp)
	{
		free(File_Name);
		return false;
	}

    for (i = n_x_a; i < n_x_b; i++ )
	{
		fprintf(fp," %6.12f",Tomb[i]);
	}
	fclose(fp);

	free(File_Name);
	return true;
}


///////////////////////////////////////////////////////////////////////////////////////
//Save 2D matrix with double elements
///////////////////////////////////////////////////////////////////////////////////////
bool save_2D(double **Tomb, int n_x_a, int n_x_b, int n_y_a, int n_y_b, 
	     	 int n_t, char *ut_nev)
{
	int i, j;

	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	FILE *fp;
	if ((fp=fopen(File_Name, "w+")) ==NULL)
	{
		free(File_Name);
		return false;
	}
    else
	{
		for (i=n_x_a; i<n_x_b; i++)
		{	
			for (j=n_y_a; j<n_y_b; j++)
			{	
				fprintf(fp,"%6.12f  ",Tomb[i][j]);
			}
			fprintf(fp,"\n");
		}
		fclose(fp);
	}

	free(File_Name);
	return true;
}


///////////////////////////////////////////////////////////////////////////////////////
//Save 2D matrix with double elements in binary file 
///////////////////////////////////////////////////////////////////////////////////////
bool save_2D_binary(double **&Tomb, int &nx, int &ny, int &n_t, char *&ut_nev)
{
	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	FILE *fp;

	if ((fp=fopen(File_Name, "wb")) == NULL)
		return false;
    else
	{
		fwrite(Tomb[0],sizeof(double),nx*ny,fp);
	    fclose(fp);
	}

	free(File_Name);
	return true;
}
/*
///////////////////////////////////////////////////////////////////////////////////////
//Reads 2D matrix with double elements from binary file 
///////////////////////////////////////////////////////////////////////////////////////
BOOL load_2D_binary(double **Tomb, int &nx, int &ny, char * ut_nev)
{
	FILE *fp;

	if ((fp=fopen(ut_nev, "rb")) == NULL)
		return FALSE;
    else
	{
		fread(Tomb[0],sizeof(double),nx*ny,fp);
	    fclose(fp);
	}

	return TRUE;
}*/


///////////////////////////////////////////////////////////////////////////////////////
//Save 2D matrix with int elements
///////////////////////////////////////////////////////////////////////////////////////
bool save_2D_int(int **Tomb, int &n_x_a, int &n_x_b, int &n_y_a, int &n_y_b, 
	     	 int &n_t, char * ut_nev)
{
	int i, j;

	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	FILE *fp;
	if ((fp=fopen(File_Name, "w+")) ==NULL)
		return false;
    else
	{
		for (i=n_x_a; i<n_x_b; i++)
		{	
			for (j=n_y_a; j<n_y_b; j++)
			{	
				fprintf(fp,"%d  ",Tomb[i][j]);
			}
			fprintf(fp,"\n");
		}
		fclose(fp);
	}

	return true;
}

///////////////////////////////////////////////////////////////////////////////////////
//Load 2D matrix with double elements
///////////////////////////////////////////////////////////////////////////////////////
int load_2D(double **Tomb, int &nx, int &ny, char * ut_nev)
{
	FILE *fname;

	char str[100];
	int i = 0;
	int j = 0;
	if( (fname = fopen( ut_nev, "r" )) != NULL )
	{
		while ( fscanf(fname,"%s", &str) != EOF )
		{
			if (i==nx)
					return -3; //wrong dimensions

			Tomb[i][j] = atof(str);
			if (Tomb[i][j] == 0.0)
			{
				int k = 0;
				while(str[k] != '\0')
				{
					if ( !( (str[k]>='0' && str[k]<='9') || 
							(str[k] == 46 && 
							  ( isdigit(str[k-1]) != 0 || isdigit(str[k+1]) != 0) ) ))
					    return -2; //wrong file content
					k++;
				}
			}
			j++;
			if (j == ny)
			{
				j = 0;
				i++;
			}
		}
		fclose(fname);
	}
	else
	{
 		return -1; //faild to open the file
	}

	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////
//Load 2D matrix with int elements
///////////////////////////////////////////////////////////////////////////////////////
int load_2D_int(int **Tomb, int &nx, int &ny, char * ut_nev)
{
	FILE *fname;

	char str[100];
	int i = 0;
	int j = 0;
	if( (fname = fopen( ut_nev, "r" )) != NULL )
	{
		while ( fscanf(fname,"%s", &str) != EOF )
		{
			if (i==nx)
					return -3; //wrong dimensions

			Tomb[i][j] = atoi(str);
			if (Tomb[i][j] == 0)
			{
				int k = 0;
				while(str[k] != '\0')
				{
					if ( !( str[k]>='0' && str[k]<='9') )
					    return -2; //wrong file content
					k++;
				}
			}
			j++;
			if (j == ny)
			{
				j = 0;
				i++;
			}
		}
		fclose(fname);
	}
	else
	{
 		return -1; //faild to open the file
	}

	if (i!=nx)
			return -3; //wrong dimensions

	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////
//Load 2D Geom matrix with int elements
///////////////////////////////////////////////////////////////////////////////////////
int load_2D_Geom_int(int **Tomb, int nx, int ny, char *ut_nev)
{
	FILE *fname;
	long i, j, n_x, n_y;

	char str[100];
	if( (fname = fopen( ut_nev, "r" )) != NULL )
	{
		long cik = 0;
		while ( fscanf(fname,"%s", &str) != EOF )
		{
			//the first line contains the dimensions of the matrix
			switch (cik)
			{
				case 0: //the x dimension
					n_x = atoi(str);
					if (n_x != nx)
					{
						//"wrong file content\n"
						return 2;
					}
					cik = 1;
					break;
				case 1: //the y dimension
					n_y = atoi(str);
					if (n_y != ny)
					{
						//"wrong file content\n"
						return 2;
					}
					cik = 2;
					break;
				case 2:
					i = atoi(str);
					if (i >= nx)
					{
						//"wrong file content\n"
						return 2;
					}
					cik = 3;
					break;
				case 3:
					j = atoi(str);
					if (j >= ny)
					{
						//"wrong file content\n"
						return 2;
					}
					cik = 4;
					break;
				case 4:
					Tomb[i][j] = atoi(str);
					cik = 2;
					break;
				}
		}

		fclose(fname);
	}
	else
	{
 		return 1; //"faild to open the file"
	}

	return 0;
}

///////////////////////////////////////////////////////////////////////////////////////
//Load 3D Geom matrix with int elements
///////////////////////////////////////////////////////////////////////////////////////
int load_3D_Geom_int(int ***Tomb, int &nx, int &ny, int &nz, char * ut_nev)
{
	    FILE *fname;
		int i, j, k, n_x, n_y, n_z;
	
		char str[100];
		if( (fname = fopen( ut_nev, "r" )) != NULL )
		{
			int cik = 0;
			while ( fscanf(fname,"%s", &str) != EOF )
			{
				//the first line contains the dimensions of the matrix
				switch (cik)
				{
					case 0: //the x dimension
						n_x = atoi(str);
						if (n_x != nx)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 1;
						break;
					case 1: //the y dimension
						n_y = atoi(str);
						if (n_y != ny)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 2;
						break;
					case 2: //the z dimension
						n_z = atoi(str);
						if (n_z != nz)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 3;
						break;
					case 3:
						i = atoi(str);
						if (i >= nx)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 4;
						break;
					case 4:
						j = atoi(str);
						if (j >= ny)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 5;
						break;
					case 5:
						k = atoi(str);
						if (k >= nz)
						{
							//"wrong file content\n"
							return -2;
						}
						cik = 6;
						break;
					case 6:
						Tomb[i][j][k] = atoi(str);
						cik = 3;
						break;
					}
			}

			fclose(fname);
		}
		else
		{
 			return -1; //"faild to open the file"
		}

	return 1;
}


///////////////////////////////////////////////////////////////////////////////////////
//Save xy plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
bool save_3D_xy(double ***&Tomb, int n_x_a, int n_x_b, int n_y_a, int n_y_b, 
            	int nz,  int n_t, char *&ut_nev)
{
	int i, j;
	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	FILE *fp;
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i_xy", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	if ((fp=fopen(File_Name, "w+")) ==NULL)
	{
		free(File_Name);
		return false;
	}
    else
	{
		for (i=n_x_a; i<n_x_b; i++)
		{	
			for (j=n_y_a; j<n_y_b; j++)
			{	
				fprintf(fp,"%6.12f  ",Tomb[i][j][nz]);
			}
			fprintf(fp,"\n");
		}
		fclose(fp);
	}
	
	free(File_Name);
	return true;
}


///////////////////////////////////////////////////////////////////////////////////////
//Save xz plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
bool save_3D_xz(double ***&Tomb, int n_x_a, int n_x_b, int ny,  int n_z_a, 
				int n_z_b, int n_t, char *&ut_nev)
{
	int i, k;

	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	FILE *fp;
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i_xz", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	if ((fp=fopen(File_Name, "w+")) ==NULL)
	{
		free(File_Name);
		return false;
	}
    else
	{
		for (i=n_x_a; i<n_x_b; i++)
		{	
			for (k=n_z_a; k<n_z_b; k++)
			{	
				fprintf(fp,"%6.12f  ",Tomb[i][ny][k]);
			}
			fprintf(fp,"\n");
		}
		fclose(fp);
	}

	free(File_Name);
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////
//Save yz plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
bool save_3D_yz(double ***&Tomb, int nx,  int n_y_a, int n_y_b, int n_z_a, 
				int n_z_b, int n_t, char *&ut_nev)
{	
	int j, k;
	
	char *File_Name = NULL;
	File_Name =(char *) calloc(512,sizeof(char));
	if (!File_Name)
	{
		return false;
	}
	
	FILE *fp;
	
	strcat(File_Name,ut_nev);
	char buffer[256];
	sprintf(buffer, "_%i_yz", n_t);
	strcat(File_Name,buffer);
	strcat(File_Name,".dat");

	if ((fp=fopen(File_Name, "w+")) ==NULL)
	{
		free(File_Name);
		return false;
	}
    else
	{
		for (j=n_y_a; j<n_y_b; j++)
		{	
			for (k=n_z_a; k<n_z_b; k++)
			{	
				fprintf(fp,"%6.12f  ",Tomb[nx][j][k]);
			}
			fprintf(fp,"\n");
		}
		fclose(fp);
	}

	free(File_Name);
	return true;
}

/*
///////////////////////////////////////////////////////////////////////////////////////
//Save xy plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
BOOL save_3D_binary(double ***Tomb, int &n_x, int &n_y, int &n_z, int &n_t, char * ut_nev, double &norm_fact)
{
	CString File_Name;
	FILE *fp;

	File_Name.Format("%s_%dsz%dsz%d_%d.dat",ut_nev,n_x,n_y,n_z,n_t);

	if ((fp=fopen(File_Name, "wb")) == NULL)
		return FALSE;
    else
	{
		fwrite(Tomb[0],sizeof(double),n_x*n_y*n_z,fp);
	    fclose(fp);
	}

	return TRUE;
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -