C 如何在verifone中读取和写入.dat文件
我想在verifone中读写一个文本或.dat文件来存储数据。 我怎样才能做到? 这是我的密码C 如何在verifone中读取和写入.dat文件,c,point-of-sale,verifone,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,s
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_create
(如果文件已经存在,则截断/删除以前的内容)O_TRUNC
(如果文件已存在,则返回错误值)O_exc
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;
}
请注意,内部文件指针已经在“curPosition”处,但这样做允许我在操作该位置时随意向前和向后移动。因此,例如,如果我想回到以前的数据结构,我只需按如下方式设置“curPosition”:
curPosition -= 2 * sizeof(SomeDataStruct);
如果我不想跟踪“curPosition”,我还可以执行以下操作,将内部文件指针移动到正确的位置:
lseek(file, - (2 * sizeof(SomeDataStruct)), SEEK_CUR);
您可以选择最适合您的方法。您很少需要同时读取和写入文件,因此在熟悉文件I/O之前,不要使用
+
模式。相反,请使用“w”
打开文件,写入文件,然后关闭文件。然后用“r”
再次打开它,从中读取并关闭它。养成检查返回值是否存在错误条件的习惯,并将每个fopen
与相应的fclose
配对。如何在该文件中搜索?用wich命令?你想要什么根本不清楚。我猜你关于阅读和写作的问题是错误的。(如果您想拥有类似数据库的结构,请将其加载到内存中,在内存中对数据库进行操作,然后再次将其提交到文件中。无论如何,请阅读一本关于C中文件I/O的好教程。)
lseek(file, - (2 * sizeof(SomeDataStruct)), SEEK_CUR);