lua向文件中写入数据,进行记录

function readfile(path)
      local file = io.open(path, "r")
      if file then
        local content = file:read("*a")
        io.close(file)
        return content
      end
      return nil
end

function writefile(path, content, mode)
      mode = mode or "w+b"
      local file = io.open(path, mode)
      if file then
        if file:write(content) == nil then return false end
        io.close(file)
        return true
      else
        return false
      end
end
--文件读写测试
local a = {x=1,y=2,z=3}
local str = json.encode(a)                --将lua表编码为json格式字符串
local path = cc.FileUtils:getInstance():getWritablePath()
    path = path..filename                 --得到可读写文件的路径
    writefile(path,str,r)                 --将数据写入文件
local b = readfile(path)                  --从文件中读取数据
    printf(b)
end

2. 文件操作示例代码local path = cc.FileUtils:getInstance():getWritablePath()cc.FileUtils:getInstance():writeToFile({key="value"}, paht.."filename")     --读取数据的方式print(ret.key) lcoal ret = cc.FileUtils:getInstance():getValueMapFromFile(Path.."filename")  --读取数据的方式2print(str)local str = cc.FileUtils:getInstance():getStringFromFile(path.."filename")
时间: 2024-08-10 20:42:52

lua向文件中写入数据,进行记录的相关文章

计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。

//给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量tfname中. //重难点:对文件的处理.如何判断文件是否达到末尾,如何往文件中写入数据. 1 #include <stdio.h> 2 #include <stdlib.h> 3 int fun(char *source, char *target) 4 { FILE *fs,*f

路径问题 Global文件中写入错误日志记录

“~”表示Web 应用程序根目录,“/”也是表示根目录,“../”表示当前目录的上一级目录,“./”表示当前目录 1  throw抛出异常     2 执行OnActionExecuted 方法   3执行 Global  中的 Application_Error 方法写入日志 global文件中写入  错误日志记录 protected void Application_Error(object sender,EventArgs e) { Exception lastError = Server

java实现赋值excel模板,并在新文件中写入数据,并且下载

/** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new File("d:/ss.xls"); //新文件写入数据,并下载***************************************************** InputStream is = null; HSSFWorkbook workbook = null; HSSFSh

第十六章,向txt文件中写入数据(C++)

#include <iostream> #include <fstream> int main(int argc, char** argv) { //app是追加的意思,append //盘符后面一定是双斜杠 \\ //没有这个文件,会自动创建 std::ofstream outfile("e:\\123.txt",std::ios::app); if(outfile.is_open()){ outfile<<"ccccc"; /

java RandomAccessFile 向文件中写入数据,怎么样不覆盖原来的数据

import java.io.IOException;import java.io.RandomAccessFile;public class RandomFileAccess {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("d:/abc.txt", "rw");// 你需要使用seek设置文件的偏移量ra

java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

1 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.

POI向Excel中写入数据及追加数据

import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.ArrayList; import java.

SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)

1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地址码 . FlightID Varchar(10) NULL, --航班号) 2/ 建立存储过程 USE DF17DataProIF EXISTS (SELECT * FROM SYS.PROCEDURES WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[BulkDataP

读取文件中的数据(以结构体存放)

/* *读取文件中的数据(数据以结构体存放) */ #include<iostream> #include <fstream> //#define Field 31 //field_anal number #define Field 15 //field_post number using namespace std; //the level restore certain level data //level_z show the level struct Level { int