//*********************************************************
// Labwindows/cvi8.5 使用ArrayToFile,fileToArray读写数据文件
// 改写自 Labwindows 自带的例程
// szlihongtao
// 2010-06-12
//*********************************************************
/*---------------------------------------------------------------------------*/
/* */
/* FILE: arrayfile.c */
/* */
/* PURPOSE: This example illustrates how to use the Formatting and IO */
/* functions ArrayToFile and fileToArray to write/read a data file. */
/* */
/*---------------------------------------------------------------------------*/
//******************************************************************************
#include <cvirte.h>
#include <userint.h>
#include <formatio.h>
#include <utility.h>
#include <stdlib.h>
#include "arrayfile.h"
#include "myMacro.h"
#include <analysis.h>
//******************************************************************************
#define COUNT 100 // 波形的点数
static int handle;
static int wave[COUNT];
static char file_name[MAX_PATHNAME_LEN];
//******************************************************************************
/* This is the application's entry-point. */
//******************************************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
handle = LoadPanel (0, "arrayfile.uir", Examp1);
SetPanelAttribute(handle, ATTR_TITLE, "Example - Write to Ascii File"); /* 更改面板最上面的文字提示 */
/* 这句话没有什么用处 */
DisplayPanel (handle);
RunUserInterface ();
DiscardPanel (handle);
CloseCVIRTE ();
return 0;
}
//******************************************************************************
/* Plot some data to the Graph control. */
//******************************************************************************
int CVICALLBACK Plot (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
int i;
if (event == EVENT_COMMIT)
{
for (i=0;i<COUNT;i++) /* 产生100个随机数据 */
wave = rand();
DeleteGraphPlot (handle, Examp1_Graph, -1, 1); /* 先删除显示 */
PlotY (handle, Examp1_Graph, wave, COUNT, VAL_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); /* 画图 */
SetCtrlAttribute (handle, Examp1_Save, ATTR_DIMMED, 0); /* 0---允许使用保存按钮 */
}
return 0;
}
//******************************************************************************
/* This function brings up a File Selection dialog and allows you to enter a */
/* file name with a .dat extension. After the file name is entered or */
/* selected, that file is opened and assigned a file handle. */
/* This file handle is used anytime action is performed on the file. The */
/* file is written using the ArrayToFile function, then closed. */
//******************************************************************************
int CVICALLBACK Save (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
int i;
int fileType;
if (event == EVENT_COMMIT)
{
if (FileSelectPopup ("", "*.dat", "*.dat;*.bin", "保存文件", VAL_OK_BUTTON, 0, 0, 1, 0, file_name) > 0) /* file_name为选择的文件名称 */
/*
0-----VAL_NO_FILE_SELECTED 没有选择文件
1-----VAL_EXISTING_FILE_SELECTED 选择硬盘上已经有的文件
2-----VAL_NEW_FILE_SELECTED 创个新的文件
*/
{
GetCtrlVal (handle, Examp1_OutputType, &fileType); /* 选择文件类型 */
ArrayToFile (file_name, wave, VAL_INTEGER, COUNT, 8, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, VAL_SEP_BY_COMMA, 18,
fileType, VAL_TRUNCATE); /* 将指针放在文件的开头,删除以前的文件内容 */
SetCtrlAttribute (handle, Examp1_Save, ATTR_DIMMED, 1); /* 不能在选择 保存 按钮了,因为已经保持了一次 */
SetCtrlAttribute (handle, Examp1_Read, ATTR_DIMMED, 0); /* 0---允许使用读出按钮 */
}
}
return 0;
}
//******************************************************************************
/* Read data from the file into our array, then plot it. */
//******************************************************************************
int CVICALLBACK Read (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
int i,status,fileType;
if (event == EVENT_COMMIT)
{
if (FileSelectPopup ("", "*.dat", "*.dat", "Name of File to Read",
VAL_OK_BUTTON, 0, 1, 1, 0, file_name) > 0)
{
GetCtrlVal (handle, Examp1_InputType, &fileType);
for (i=0;i<COUNT;i++) /* 数组清0 */
wave = 0;
FileToArray (file_name, wave, VAL_INTEGER, COUNT, 8, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, fileType);
DeleteGraphPlot (handle, Examp1_Graph2, -1, 1);
PlotY (handle, Examp1_Graph2, wave, COUNT, VAL_INTEGER,
VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1,VAL_RED);
}
}
return 0;
}
//******************************************************************************
/* Quit the application */
//******************************************************************************
int CVICALLBACK Quit (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
if (event == EVENT_COMMIT)
QuitUserInterface (0);
return 0;
}
//******************************************************************************
//******************************************************************************
//******************************************************************************
用户377235 2016-3-11 13:19
用户443155 2016-3-4 08:22
用户402594 2015-12-28 08:59
用户1851539 2015-11-4 23:03
用户1694422 2015-8-21 17:01
用户455798 2015-7-16 16:57
hzddyx_297768481 2015-5-5 17:00
自做自受 2015-5-5 11:06
用户1835224 2015-4-17 10:16
用户1834559 2015-4-10 18:04