c - 如何在 verifone 中读取和写入 .dat 文件

标签 c point-of-sale verifone

我想在 verifone 中读写文本或 .dat 文件以在其上存储数据。 我怎样才能做到? 这是我的代码

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;
}

最佳答案

“Vx 解决方案程序员手册”(“23230_Verix_V_Operating_System_Programmers_Manual.pdf”)的第 3 章都是关于文件管理的,包含了我在终端上处理数据文件时通常使用的所有功能。通读一下,我想您会找到所需的一切。

为了让你开始,你需要使用 open() 和你想要的标志

  • O_RDONLY(只读)
  • O_WRONLY(只写)
  • O_RDWR(读写)
  • O_APPEND(以文件末尾的文件位置指针打开)
  • O_CREAT(如果文件不存在则创建),
  • O_TRUNC(如果文件已经存在,则截断/删除之前的内容),
  • O_EXCL(如果文件已经存在则返回错误值)

成功后,open 将返回一个正整数,它是一个句柄,可用于后续访问该文件。失败时返回-1;

当文件打开时,您可以使用read()write() 来操作内容。

一定要调用 close() 并在完成文件后传入 open 的返回值。

你上面的例子看起来像这样:

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;
}

您的评论还询问有关搜索的问题。为此,您还需要将 lseek 与以下其中一项结合使用,指定您从哪里开始:

  • SEEK_SET — 文件开头
  • SEEK_CUR — 当前查找指针位置
  • SEEK_END — 文件结尾

例子

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.

请注意,内部文件指针已经位于“curPosition”,但这样做可以让我在操作那里的内容时随意向前和向后移动。因此,例如,如果我想回到以前的数据结构,我只需按如下方式设置“curPosition”:

curPosition -= 2 * sizeof(SomeDataStruct);

如果我不想跟踪“curPosition”,我也可以执行以下操作,这也会将内部文件指针移动到正确的位置:

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

您可以选择最适合您的方法。

关于c - 如何在 verifone 中读取和写入 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769323/

相关文章:

point-of-sale - 如何在 verifone vx520 中反转文本

c - 搜索 char** 类型的字符串

c - 为什么在 C 的 if-else 条件中传递 (!NULL) 为真?

c - 使用c编程的顺序文件访问

design-patterns - 超市系统的设计模式

emv - 惠尔丰EMV : Second Generate AC?

c - C 编程的骰子模拟器

c++ - 单元测试框架——POS应用

java - 如何在Java中实现条码扫描器?

c - 对 VeriFone 库的内存可用调用