简体   繁体   English

如何在Verifone中从.dat文件读取和写入

[英]how to read and write from .dat file in verifone

I want to read and write a text or .dat file in verifone to store data on it. 我想在verifone中读写文本或.dat文件以在其上存储数据。 How can I make it ? 我该怎么做? here is my code 这是我的代码

int main()
{
  char buf [255];
  FILE *tst;
  int dsply = open(DEV_CONSOLE , 0);
  tst = fopen("test.txt","r+");
  fputs("this text should write in file.",tst);
  fgets(buf,30,tst);

  write(dsply,buf,strlen(buf));
  return 0;
}

Chapter 3 of the "Programmers Manual for Vx Solutions" ("23230_Verix_V_Operating_System_Programmers_Manual.pdf") is all about file management and contains all the functions I typically use when dealing with data files on the terminal. “ Vx解决方案程序员手册”(“ 23230_Verix_V_Operating_System_Programmers_Manual.pdf”)的第3章都是关于文件管理的,它包含了我在终端上处理数据文件时通常使用的所有功能。 Go read through that and I think you'll find everything you need. 请仔细阅读,我想您会找到所需的一切。

To get you started, you'll want to use open() together with the flags you want 要开始使用,您需要将open()与所需的标志一起使用

  • O_RDONLY (read only) O_RDONLY (只读)
  • O_WRONLY (write only) O_WRONLY (仅写)
  • O_RDWR (read and write) O_RDWR (读写)
  • O_APPEND (Opens with the file position pointer at the end of the file) O_APPEND (以文件末尾的文件位置指针打开)
  • O_CREAT (create the file if it doesn't already exist), O_CREAT (如果尚不存在,则创建该文件),
  • O_TRUNC (truncate/delete previous contents if the file already exists), O_TRUNC (如果文件已经存在,则截断/删除先前的内容),
  • O_EXCL (Returns error value if the file already exists) O_EXCL (如果文件已经存在,则返回错误值)

On success, open will return a positive integer that is a handle which can be used for subsequent access to the file. 成功打开后,open将返回一个正整数,该整数是可用于后续访问文件的句柄。 On failure, it returns -1; 失败时,返回-1;否则,返回-1。

When the file is open, you can use read() and write() to manipulate the contents. 打开文件后,可以使用read()write()处理内容。

Be sure to call close() and pass in the return value from open when you are done with the file. 完成文件操作后,请确保调用close()并从open传递返回值。

Your example above would look something like this: 上面的示例如下所示:

int main()
{
  char buf [255];
  int tst;
  int dsply = open(DEV_CONSOLE , 0);
  //next we will open the file.  We will want to read and write, so we use
  // O_RDWR.  If the files does not already exist, we want to create it, so
  // we use O_CREAT.  If the file *DOES* already exist, we want to truncate
  // and start fresh, so we delete all previous contents with O_TRUNC
  tst = open("test.txt", O_RDWR | O_CREAT | O_TRUNC);

  // always check the return value.
  if(tst < 0)
  {
     write(dsply, "ERROR!", 6);
     return 0;
  }
  strcpy(buf, "this text should write in file.")
  write(tst, buf, strlen(buf));
  memset(buf, 0, sizeof(buf));
  read(tst, buf, 30);
  //be sure to close when you are done
  close(tst);

  write(dsply,buf,strlen(buf));
  //you'll want to close your devices, as well
  close(dsply);
  return 0;
}

Your comments also ask about searching. 您的评论还询问有关搜索的信息。 For that, you'll also need to use lseek with one of the following which specifies where you are starting from: 为此,您还需要将lseek与以下其中一项指定您从哪里开始:

  • SEEK_SET — Beginning of file SEEK_SET文件开头
  • SEEK_CUR — Current seek pointer location SEEK_CUR —当前SEEK_CUR指针位置
  • SEEK_END — End of file SEEK_END —文件结尾

example

SomeDataStruct myData;
...
//assume "curPosition" is set to the beginning of the next data structure I want to read
lseek(file, curPosition, SEEK_SET);
result = read(file, (char*)&myData, sizeof(SomeDataStruct));
curPosition += sizeof(SomeDataStruct);
//now "curPosition" is ready to pull out the next data structure.

NOTE that the internal file pointer is already AT "curPosition", but doing it this way allows me to move forward and backward at will as I manipulate what is there. 请注意,内部文件指针已经位于AT“ curPosition”上,但是通过这种方式,我可以在操纵其中的内容时随意向前和向后移动。 So, for example, if I wanted to move back to the previous data structure, I would simply set "curPosition" as follows: 因此,例如,如果我想返回到先前的数据结构,只需设置“ curPosition”,如下所示:

curPosition -= 2 * sizeof(SomeDataStruct);

If I didn't want to keep track of "curPosition", I could also do the following which would also move the internal file pointer to the correct place: 如果我不想跟踪“ curPosition”,也可以执行以下操作,该操作还将内部文件指针移动到正确的位置:

lseek(file, - (2 * sizeof(SomeDataStruct)), SEEK_CUR);

You get to pick whichever method works best for you. 您可以选择最适合您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM