我正在使用 Qt creator 做一个项目。我有 3 个屏幕,每个屏幕有 4 个按钮。当单击第一个按钮时,它会将 0 写入文件(字符),依此类推到 3。当我到达最后一个屏幕(4.屏幕)时,我将从文件中读取并显示按钮的输入3个字符。
void fileOperation::openFileWrite(char x, off_t s)
{
int fd;
char c[2] = {x};
fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY, 0666);//open file
if(fd == -1)
cout << "can't open file" << endl;
else
{
lseek(fd, s, SEEK_SET);//seek at first byte
write(fd, (void*)&c, 2);//write to file
}
//syncfs(fd);
::close(fd);
}
QString fileOperation::openFileRead()
{
int fd;
QString str;
char c[4];
fd = open("/home/stud/txtFile", O_RDWR);
lseek(fd, 0, SEEK_SET);
read(fd, (void*) &c, 4);
str = QString(c);
return str;
::close(fd);
}
当我关闭应用程序并使用按钮的新输入再次打开它时,它会在最后一个屏幕中显示上一个输入。任何解决此问题的建议或帮助。