【发布时间】:2016-05-10 10:55:35
【问题描述】:
我正在 WinCE7 中开发一个应用程序。该应用程序包括串行 com 端口和文件 IO 操作。有一个嵌入式设备连接了串口。我需要检查设备上输入的状态并将其详细信息保存在文件中。比方说,如果输入1为高,那么我需要在串口上写Input 1 HIGH,并将其保存在文件中。
现在要在文件中写入数据,我正在使用 fprintf 和 fopen 函数。代码如下:
main()
{
// some code to initialize serial port
FILE * fp;
fp= fopen ("Logs.txt", "w+"); //-------> create a file named as Logs.txt
while(1)
{
if(Input1 == TRUE)
{
serialPort.Send("Input 1 HIGH");
fprintf(fp,"%s","Input 1 HIGH"); //-------> saving data in file
}
if(Input2 == TRUE)
{
serialPort.Send("Input 2 HIGH");
fprintf(fp,"%s","Input 2 HIGH"); //-------> saving data in file
}
//same goes for rest of the inputs
}
fclose(fp); //----------> closing the file
}
现在使用fprintf将数据写入文件后,我们需要使用fclose()关闭文件。但是因为我必须持续监控我使用的输入状态,而(1)由于我的控制没有达到fclose(fp)。因此该文件没有关闭,它变得损坏。当我打开文件查看保存的数据时,它给了我以下错误:
如何正确使用flcose()和fprintf()将数据写入文件?
【问题讨论】:
标签: c file windows-ce