Lua获取文件长度和判断文件是否存在函数分享_Lua

获得文件长度

复制代码 代码如下:

 function length_of_file(filename)
  local fh = assert(io.open(filename, "rb"))
  local len = assert(fh:seek("end"))
  fh:close()
  return len
end

判断文件是否存在

复制代码 代码如下:

function file_exists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end

时间: 2024-09-01 14:58:18

Lua获取文件长度和判断文件是否存在函数分享_Lua的相关文章

Lua获取文件长度和判断文件是否存在函数

  这篇文章主要介绍了Lua获取文件长度和判断文件是否存在函数分享,需要的朋友可以参考下 获得文件长度 代码如下: function length_of_file(filename) local fh = assert(io.open(filename, "rb")) local len = assert(fh:seek("end")) fh:close() return len end 判断文件是否存在 代码如下: function file_exists(pat

php is_file file_exists判断文件是否存函数

<?php  mysql_connect('localhost','root','root') or die('remote server cant' connect');  mysql_select_db('www.111cn.net') or('selected not exists!');  $sql = "Select borough_thumb,id from fke_borough where borough_thumb<>'' and isnew =1 "

Lua判断字符串中包含中文字符的方法和计算字符串宽度函数分享_Lua

一.判断字符串中包含中文字符的方法 遍历数组,对每个字节使用string.byte(),发现有大于127的,就是汉字,可以参照下面的代码. 二.计算字符串宽度函数 复制代码 代码如下: -- 计算字符串宽度   local str = "Jimmy: 你好,世界!" local fontSize = 20 local lenInByte = #str local width = 0   for i=1,lenInByte do     local curByte = string.by

使用Lua编写Web端模板引擎的实例代码分享_Lua

ltemplate.lua local insert = table.insert local remove = table.remove local concat = table.concat local format = string.format local loaded = {} local partten = "(.-){#([^#].-[^#])#}()" local content = {} local cur_content = nil local function o

Lua字符串库中的几个重点函数介绍_Lua

在<Lua中的一些库>中也说到了,要对string库的模式匹配进行单独的讲解.对于字符串的处理,对于任何语言的学习来说,都是一个难点,而且也是一个必会的知识点.给你一个字符串,让你按照某种需求进行处理,你不会,那是多么尴尬的一件事情.所以,看完<Lua中的一些库>和这篇文章之后,我争取做到让你在处理字符串时,不再感到捉襟见肘,不再尴尬. 说到Lua中的模式匹配,基本上就是围绕着以下几个函数展开的: 1.find: 2.match: 3.gsub: 4.gmatch. 我的总结也就是

c++-C++使用ifstream如何获取文件长度(具体数值)

问题描述 C++使用ifstream如何获取文件长度(具体数值) rt,如何获取一个文件的实际长度(字节即可),但是我不是要打印在屏幕上,而是要存在 long long型的变量里面.网上给的streampos的方法虽然能得到长度但是,无法将长度转存到一个long long型里面(强转是木有用的),虽然streampos里面有个Fpos正好能指示长度然而不幸的是人家是私有的,无法读取.所以还请大神帮帮忙,要用ifstream,并且能够读取文件长度,多谢. 解决方案 http://blog.163.

PHP判断文件是否存在

PHP判断文件是否存在,可使用如下代码: //判断远程文件是否存在   function remote_file_exists($url) {       $executeTime = ini_get('max_execution_time');       ini_set('max_execution_time', 0);       $headers = @get_headers($url);       ini_set('max_execution_time', $executeTime)

FSO使用教程8 -- 如何使用FSO判断文件是否存在

fso|教程|是否存在 如何使用FSO判断文件是否存在-FSO使用教程8 写入数据到文件的方法有WriteLine及Write两种,以下详细介绍这两种的使用方法: 八.如何使用FSO判断文件是否存在: 方法:FileExists 调用格式:(右文件存在,则返回X等于True,否则等于False) X = FileSystemObject名.FileExists(文件名) 源码示例:(检查与ASP文件同一目录下的File1.txt是否存在) <%'=========================

C++:判断文件夹(folder)是否存在(exist)

写入程序, 需要在文件夹中写入数据, 如果文件夹不存在, 则无法写入, 在程序入口需要判断; 由于属于系统层, Windows的两种解决方法. 参考: http://stackoverflow.com/questions/8233842/how-to-check-if-directory-exist-using-c-and-winapi 1. GetFileAttributesA()函数 DWORD d = GetFileAttributesA(const char* filename); #i