我的程序有一个循环,它运行initUSB(),然后多次运行writeEssentials()。initUSB()是一个将USB挂载到目录的函数。writeEssentials()是一个函数,它打开一个文件并将其附加到数据中,然后关闭该文件。
在程序初始运行一分钟左右之后,程序将报告文件系统为“只读”,并拒绝写入更多的数据,直到initUSB()再次运行为止。不管天气如何,我都会将fprintf()放到文件指针中。作为一种临时解决方案,如果驱动器变成只读的,我让writeEssentials()重新装入它。这是可行的,但我不愿每分钟重新安装驱动器。
为什么会发生这种情况,以及如何修复此错误?
该程序运行在Debian嵌入式Linux系统上,在TS-7800上。
InitUSB:
int initUSB(){
int i;
FILE * filecheck = fopen(HMITelemCheckFile, "r");
for(i = 0; i < 26; i++) {
char usbMountFromPathTry[256];
char sdanum[5];
strcpy(usbMountFromPathTry, usbMountFromPath);
sprintf(sdanum, "%c1", i+'a');
strcat(usbMountFromPathTry, sdanum);
if(!mount(usbMountFromPathTry, usbMountToPath, "vfat", (long)NULL, NULL)){
printf("Mount successful\n");
return 1;
} else if(!mount(usbMountFromPathTry, usbMountToPath, "vfat", MS_REMOUNT, NULL)){
printf("Mount successful\n");
return 1;
}
printf("Mount error: ");
printf("%s\n", usbMountFromPathTry);
}
printf("Mount ERROR\n");
return 0;
}writeEssentials():
void writeEssentials(){
FILE * file = fopen(usbMountEssentials, "a+");
fflush(file);
perror("file");
if(file == NULL){
initUSB();
printf("null file\n");
return;
}
fprintf(file, "\n%s, ", getDate());
fprintf(file, "%s, ", getTime());
fprintf(file, "%1.2f, ", getSpeed());
fprintf(file, "%d, ", getRPM());
fprintf(file, "%d, ", getRegen());
fprintf(file, "%d, ", getAirgap());
fprintf(file, "%d, ", getBattery());
fprintf(file, "%.2f, ", *(getADCTemps()+COMPUTER_BOX_TEMP_INDEX));
fprintf(file, "%.2f, ", *(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*0)));
fprintf(file, "%.2f, ", *(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*1)));
fprintf(file, "%.2f, ", *(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*2)));
fprintf(file, "%.2f, ", *(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*0)));
fprintf(file, "%.2f, ", *(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*1)));
fprintf(file, "%.2f, ", *(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*2)));
fprintf(file, "%.2f, ", *(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*3)));
fprintf(file, "%s, ", getLat());
fprintf(file, "%s, ", getLong());
int i;
for(i = 0; i < getNumErrors(); i++){
//fprintf(file, "%s, ", getErrorText(*(getErrors()+i)));
}
fclose(file);
perror("close file error");
}发布于 2012-06-13 01:40:20
检查dmesg。文件系统自发地进行只读通常意味着检测到了一些损坏,因此内核将FS设置为只读,以保护它免受进一步的损害。
https://stackoverflow.com/questions/11007124
复制相似问题