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

📄 file_demo.c

📁 tp850掌机源码文件操作例子,C语言写的,可以
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************/
/* file_demo.c
   文件系统函数操作:
   在exist菜单中,首先通过键入值判断文件是否存在,若不存在则创建相应的文件;
   在open菜单中,列出已经判断存在的文件,通过选择可以实现对应文件6种不同方式的打开;
   在seek菜单中, 确定文件指针的偏移量,可以选择从文件头,文件现在的指针位置以
      及文件尾三种方式进行定位;
   在read和write菜单中,分别列出以对应方式打开的文件:
   在read菜单中,可以得到文件的长度,文件的指针偏移量,以及每按一次读出10个字节的文件内容并显示;
   在write菜单中,可以选择键入一定的文件内容或者是键入特定的11个字节的字符串;
   

   Copyright (c) 2006 by Thinta Co.LTD
   All rights reserved 

                                                             */
/************************************************************************/
#include "..\inc\Bio_Func.h"
#include "..\inc\key.h"
#include <STRING.H>
#include <STDIO.H>

#define FALSE 0x00
#define TRUE  0x01

#define NORMALCOLOR 1
#define NONECOLOR   0

#define CHINESEMODE	1
#define ENGLISHMODE	0



/****************************主菜单函数声明****************************/
U8 file_tst(void);


/* ************************菜单子目录实现函数声明*************************/
void f_exist(void);
void f_open(void);
void f_seek(void);
void f_read(void);
void f_write(void);
void f_close(void);



/****************************其他函数声明****************************/
S32	_read_data(void);
U8 string_key(U8* str, U8 len_str);
void row_name(U8 px, U8 py, U8 row, U8* name);
void _prtf(U8 x, U8 y, U8* flg, S32 data);


void _PutString( U8 x,U8 y,U8 *str )
{
	_gotoxy(x,y);
	_putstr(str);
}


/*****************************全局变量定义*****************************/
FILE *g_fp[8]           = NULL ;
U8   g_filename[8][36]  = {0}  ;
U8	 g_opennum          = 0    ;			    //记录现在使用的是第几个
U8	 g_opencount        = 0    ;			    //记录打开的文件的总数
U8	 g_open[8]          = {0}  ;			    //和fp[8]相对应,0-没有打开的文件, 1-有打开的文件
U8   g_testcount        = 0    ;                //证明存在的文件总数
U8   g_readorwrite[8]   = {0}  ;                //记录文件是以读打开还是写打开, 1-读, 2-写,3-创建,4-其他
U8   g_readcount        = 0    ;                //读文件个数
U8   g_writecount       = 0    ;                //写文件个数	


void main (void)
{
	//系统初始化
	InitHeap() ;
	_sysinit();
	_file_sys_init();	
	_setscreenmode(CHINESEMODE);
	_setdispcolor(NORMALCOLOR);
	_setkeymode(2);

	//显示首页
	_cls();
	_PutString(2, 2, "文件系统函数使用");
	_PutString(3, 4, "时间:06.05.18");
	_PutString(1, 6, "By Thinta Co.LTD");

	//按键进入菜单选项页
	_bioskey(0);	
	while(1)
	{
		//运行菜单程序,若按退出键则退出程序
		if(!file_tst())
			break;
	}
	
}

U8 file_tst(void)
{
	U8* fMenu[] = { "1._fexist", "2._fopen", "3._fseek", 
		"4._fread", "5._fwrite", "6._fclose"};         //菜单内容
	S16	fChoice;                                                  //菜单按键选择

  //建立主菜单
	while(1)
	{
		_cls();
		_PutString(0, 1, "     -file func-     ");

		fChoice = PopUpMenu(5, 2, 14, 9, fMenu, "123456", 6, 6, fChoice, 0, 0, NULL, 0 );
	
		switch(fChoice)
		{
		case 0 : return 0;
			break;
		case 1 : f_exist();
			break;
		case 2 : f_open();
			break;
		case 3 : f_seek();
			break;
		case 4 : f_read();
			break;
		case 5 : f_write();
			break;
		case 6 : f_close();
			break;
		default:
			return 0;			

		}
	}

	_fcloseall();
}

void f_exist(void)
{
	U8    sName[10] = {0} ;  //文件名
    U8    sSuf[4]   = {0} ;  //后缀名
	U8*    dir      = NULL;  //路径
	FILE* pfile     = NULL;  //文件指针

	_cls();

	//路径初始化
	dir = malloc(30);
	memset(dir, 0, 30);
	strcpy(dir, "d:\\");	
	_PutString(0, 0, "当前路径:");
	_putstr(dir);

	//键入文件名
	_PutString(0, 1, "文件名:");
	if (string_key(sName, 10) == 0)
	{
		free(dir);
		return;
	}	
	
	//键入后缀名
	_PutString(0, 2, "后缀名:");
	if (string_key(sSuf, 4) == NULL)
	{
		free(dir);
		return;
	}
	
	//得到全路径名
	strcat(dir, sName);
	strcat(dir, ".");
	strcat(dir, sSuf); 	
	_PutString(0, 4, dir);	

	//判断文件是否存在,若不存在则创建,并且将文件名记入g_filename[]
	_gotoxy(0, 5);	
	if( !_fileexist(dir))
	{
		memset(g_filename[g_testcount],0,36);
		memcpy(g_filename[g_testcount], dir, strlen(dir));//记入已经证实存在的文件
		_putstr("文件存在");
		g_testcount++;
	}
    else 
	{
		_putstr("文件不存在\n");

		pfile = _fopen( dir, "a+" );		
		if (!pfile)
			_putstr("创建文件失败!");
		else
		{	
			memset(g_filename[g_testcount],0,36);
			memcpy(g_filename[g_testcount], dir, strlen(dir));//记入新建文件
			_putstr("创建文件成功!");
			_fclose(pfile);	
			g_testcount++;			
		}		
	}

	_bioskey(0);
}

void f_open(void)
{
	S16   Key           ;                                   //按键
	U8    i, row = 1    ;                                   //row是行号
	U8    seq[8] = {0}  ;                                   //存放对应于行号的文件序数
	U8*   readmode    = NULL ;                              //记录用户选择的文件打开模式
	U8    rw_mode[6]  = {1, 2, 3, 4, 4, 4};	               //记录用户打开的不同方式,以在下面的文件读写操作中分类
	FILE* pfile      = NULL ;                              //打开文件指针


	_cls();
	_PutString(0, 1, "选择文件打开:\n");

	//输出能够打开的文件
	for ( i = 0; i < g_testcount; i++)
	{
		if( g_open[i] != 1 )
		{
			seq[row - 1] = i;
			row_name(0, row+1, row, g_filename[i]);			
            row++;
		}
	}

	Key = _bioskey(0);	
	
	if(Key >= NUMBER_1  && Key <= NUMBER_8 )
		g_opennum = seq[(U8)(Key - NUMBER_1)];
	else
		return;

	//输出选择打开的文件
	_cls();
	_PutString(0, 1, "选择的文件为:\n");
	_putstr(g_filename[g_opennum]);
	
	//选择打开方式
	_PutString(0, 3, "选择打开方式:\n");
	_putstr("1.r,  2.w,  3.a\n4.r+, 5.w+, 6.a+");
	
	Key = _bioskey(0);
	
	switch(Key)
	{
	case NUMBER_1: readmode = "r";
  		break;    
    case NUMBER_2: readmode = "w";
  		break;
    case NUMBER_3: readmode = "a";
   		break;
	case NUMBER_4: readmode = "r+";
		  break;
	case NUMBER_5: readmode = "w+";
		  break;
	case NUMBER_6: readmode = "a+";
		  break;
    default: readmode = "r";
   		break;
	}

	//打开文件	
	pfile = _fopen(g_filename[g_opennum],readmode);

	if(NULL == pfile)
		_PutString(0, 6, "打开文件失败");
	else
	{
		_PutString(0, 6, "打开文件成功");

  	    g_open[g_opennum] = 1    ;     //标志文件已打开
   	    g_fp[g_opennum]   = pfile;     //记录文件指针
   	    (g_opencount)++;   		         //打开文件个数加一
	
		//以数字方式记录文件打开方式以在读写菜单操作中进行判断
		if(Key >= NUMBER_1  && Key <= NUMBER_6 )
		{
			g_readorwrite[g_opennum]   =  rw_mode[(U8)(Key - NUMBER_1)];
		
			if (Key == NUMBER_1 || Key == NUMBER_4 || Key == NUMBER_5 
		                    	|| Key == NUMBER_6)
				g_readcount++;

			if (Key == NUMBER_2 || Key == NUMBER_3 || Key == NUMBER_4 
			                	|| Key == NUMBER_5 || Key == NUMBER_6)
				g_writecount++;			
		}
		else
			return;
	}

	_bioskey(0);
}

void f_seek(void)
{
	S16	  choice;               //菜单选择 
	U16	  whence;               //定位方式 
	S32   pos_result, key;
	U8    i, row = 1          ; //row是行号
	U8    seq[8] = {0}        ; //记录序号
	S32	  offset = 0          ; //offset是指针偏移
	char* menu[] = {"1.SEEK_SET", "2.SEEK_CUR", "3.SEEK_END"};
	
	_cls();
	_PutString(0, 1, "选择定位文件:\n");

	for(i=0;i<8;i++)
	{
		if(g_open[i]== 1)
		{
			seq[row - 1] = i;
			row_name(0, row+1, row, g_filename[i]);			
            row++;
		}
	}
	
	key = _bioskey(0);

	if(key >= NUMBER_1  && key <= NUMBER_8 )
	{
		if ((key - NUMBER_1) < g_opencount)
			g_opennum = seq[(U8)(key - NUMBER_1)];
		else 
			return;
	}
	else
		return;
	

 	_cls();
 	_PutString(0, 1, "选择定位的文件为:\n"); 
	_putstr(g_filename[g_opennum]);  		
	_PutString(0, 4, "offset:");
	
	offset = _read_data();
	
	if (offset == ESC)
		return;
			
	_cls();
	_gotoxy(0,1);	
	choice = PopUpMenu(2, 2, 13, 5, menu, "123", 3, 3, choice, 1, 0, NULL, 0);	
	switch(choice)
	{
	case 0	:	return;
		break;
	case 1	:	whence = 0;
		break;
	case 2	:	whence = 1;
		break;
	case 3	:	whence = 2;
		break;
	default	:
		break;
	}

	pos_result = _fseek(g_fp[g_opennum], offset, whence);

	if (pos_result == 0)
		_PutString(0, 7, "定位成功!");
	else
		_PutString(0, 7, "定位失败!");	

	_bioskey(0);	

}

void f_read(void)
{

⌨️ 快捷键说明

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