使用2种不同的方法来创建新文件!
使用3种不同的方法来检测文件是否存在!
//*********************************************************
// Labwindows/cvi8.5 创建新文件
// szlihongtao
// 2010-06-18
//*********************************************************
#include <cvirte.h>
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <formatio.h>
#include "toolbox.h"
#include "new.h"
//*********************************************************
static int panelHandle;
int FileHandle,f_led;
char filename[MAX_PATHNAME_LEN];
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "new.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//*********************************************************
int CVICALLBACK callback_exit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_gen1 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
FILE *fp;
fp=fopen ("1.dat", "wb+");/* 删除以前的数据,重新打开 */
fclose(fp);
break;
}
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_gen2 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
FileHandle = OpenFile ("1.dat", VAL_READ_WRITE, VAL_TRUNCATE, VAL_BINARY); // 创建一个新文件
CloseFile(FileHandle); // 关闭文件
break;
}
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_del (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
RemoveFileIfExists ("1.dat");
break;
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_exist1 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
f_led=0;
#if 0
if (FileExists("1.dat",0)==TRUE) // 判断文件是否存在
//采用这个语句的话,运行不正常,不知道为什么????
#else
GetProjectDir (filename);
strcat (filename, "\\1.dat"); // 完整的文件名
if (FileExists(filename,0)==TRUE) // 判断文件是否存在
#endif
f_led=1;
SetCtrlVal(panel,PANEL_LED,f_led);
break;
}
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_exist2 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
long FileSize;
f_led=0;
if (GetFileInfo ("1.dat", &FileSize)==TRUE) // 文件存在
f_led=1;
SetCtrlVal(panel,PANEL_LED,f_led);
break;
}
default:
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK callback_exist3 (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
FILE *fp;
f_led=0;
fp=fopen ("1.dat", "r");
if (fp!=NULL) // 文件存在
{
f_led=1;
fclose(fp);
}
SetCtrlVal(panel,PANEL_LED,f_led);
break;
}
default:
break;
}
return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************
用户436075 2012-12-19 16:22