windows下VS2010中使用netcdf的C++接口实现.nc文件的读写
2014-05-07 11:32
826 查看
一、下载所需要的资源
a.使用的netcdf的C++接口版本:netcdf-cxx-4.2.tar.gz,下载地址:http://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp
b.下载需要的库文件:netCDFcpp.zip,下载地址:http://s.pudn.com/search_hot.asp?k=NetCDF#
二、项目的创建
a.解压下载的netcdf-cxx-4.2.tar.gz文件,得到cxx,example,m4和man4文件,还有一些其他配置文件;
b.将解压后文件中的config.h.in这个文件名改成config.h;
c.创建工程项目,然后以添加现有项的方式分别将需要的config.h和cxx文件夹中的ncvalues.h,netcdfcpp.h和ncvalues.cpp,netcdf.cpp这五个文件添加到工程头文件和源文件中;
三、项目的配置
a.包含文件:在VS项目属性—VC++目录—包含目录,添加config.h,netcdfcpp.h,ncvalues.h和netcdf.h头文件所在的位置;
b.库目录:VS项目属性—VC++目录—库目录,添加netcdf.lib库文件的位置;
VS项目属性—链接器—输入—附加依赖项,添加netcdf.lib库文件;
VS项目属性—链接器—常规—附加库目录,添加bin文件中的netcdf.dll路径;
四、测试成功与否
a.NC文件的创建:创建simple_xy.nc,并写入数据;
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
int dataOut[NX][NY];
for(int i = 0; i < NX; i++)
for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
NcFile dataFile("simple_xy.nc", NcFile::Replace);
if(!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
NcDim* xDim = dataFile.add_dim("x", NX);
NcDim*yDim = dataFile.add_dim("y", NY);
NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);
data->put(&dataOut[0][0], NX, NY);
cout << "*** SUCCESS writing example file simple_xy.nc!"<< endl;
return 0;
}
b.读取simple_xy.nc中的数据;(此时需将生成的simple_xy.nc文件放到项目所在的目录下)
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
int dataIn[NX][NY];
NcFile dataFile("simple_xy.nc", NcFile::ReadOnly);
if(!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
NcVar *data = dataFile.get_var("data");
data->get(&dataIn[0][0], NX, NY);
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
{
if(dataIn[i][j] != i * NY + j)
return NC_ERR;
cout<<dataIn[i][j]<<endl;
}
cout << "*** SUCCESS reading example file simple_xy.nc!"<< endl;
system("pause");
return 0;
}
控制台窗口会显示0,1,2,3,4,....71数据,则成功!!!
a.使用的netcdf的C++接口版本:netcdf-cxx-4.2.tar.gz,下载地址:http://www.unidata.ucar.edu/downloads/netcdf/netcdf-cxx/index.jsp
b.下载需要的库文件:netCDFcpp.zip,下载地址:http://s.pudn.com/search_hot.asp?k=NetCDF#
二、项目的创建
a.解压下载的netcdf-cxx-4.2.tar.gz文件,得到cxx,example,m4和man4文件,还有一些其他配置文件;
b.将解压后文件中的config.h.in这个文件名改成config.h;
c.创建工程项目,然后以添加现有项的方式分别将需要的config.h和cxx文件夹中的ncvalues.h,netcdfcpp.h和ncvalues.cpp,netcdf.cpp这五个文件添加到工程头文件和源文件中;
三、项目的配置
a.包含文件:在VS项目属性—VC++目录—包含目录,添加config.h,netcdfcpp.h,ncvalues.h和netcdf.h头文件所在的位置;
b.库目录:VS项目属性—VC++目录—库目录,添加netcdf.lib库文件的位置;
VS项目属性—链接器—输入—附加依赖项,添加netcdf.lib库文件;
VS项目属性—链接器—常规—附加库目录,添加bin文件中的netcdf.dll路径;
四、测试成功与否
a.NC文件的创建:创建simple_xy.nc,并写入数据;
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
int dataOut[NX][NY];
for(int i = 0; i < NX; i++)
for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
NcFile dataFile("simple_xy.nc", NcFile::Replace);
if(!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
NcDim* xDim = dataFile.add_dim("x", NX);
NcDim*yDim = dataFile.add_dim("y", NY);
NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);
data->put(&dataOut[0][0], NX, NY);
cout << "*** SUCCESS writing example file simple_xy.nc!"<< endl;
return 0;
}
b.读取simple_xy.nc中的数据;(此时需将生成的simple_xy.nc文件放到项目所在的目录下)
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
static const int NX = 6;
static const int NY = 12;
static const int NC_ERR = 2;
int main(void)
{
int dataIn[NX][NY];
NcFile dataFile("simple_xy.nc", NcFile::ReadOnly);
if(!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
NcVar *data = dataFile.get_var("data");
data->get(&dataIn[0][0], NX, NY);
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
{
if(dataIn[i][j] != i * NY + j)
return NC_ERR;
cout<<dataIn[i][j]<<endl;
}
cout << "*** SUCCESS reading example file simple_xy.nc!"<< endl;
system("pause");
return 0;
}
控制台窗口会显示0,1,2,3,4,....71数据,则成功!!!
相关文章推荐
- Windows下,实现不使用缓存的文件读写方式
- fortran使用netcdf 读写NC文件
- Windows下使用scapy+python2.7实现对pcap文件的读写操作
- 使用cwRsync实现windows下文件定时同步
- 关于windows下C++实现解压、压缩 .tar.gz文件
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- Windows使用SSH Secure Shell实现免密码登录Linux的方法以及使用scp2命令免密码下载文件
- JAVA 使用Jacob调用windows的Sapi实现文本转语音文件
- Linux下运用C/C++实现txt文件的读写
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
- 使用samba实现linux与windows文件共享
- 使用cwRsync实现windows下文件定时同步(备份)
- linux中rz, sz命令的安装与使用------实现Windows与linux文件的快捷互传
- 使用C++实现JNI接口需要注意的事项
- 在C++中如何实现文件的读写?
- C++使用ADO技术实现Excel文件的读写操作
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
- 使用C++读写TDM以及TDMS文件
- 基于C++实现文件的明文和二进制读写
- Windows下Python3实现C++多文件编译脚本(没有Make怎么办)