C 在程序运行期间将文件系统转换为只读

C 在程序运行期间将文件系统转换为只读,c,file-io,embedded-linux,usb-drive,C,File Io,Embedded Linux,Usb Drive,我的程序有一个循环,它运行initUSB(),然后多次运行writeEssentials()。 initUSB()是一个将USB装载到目录的函数。 writeEssentials()是一个函数,用于打开一个文件,并在其中附加数据,然后关闭该文件 在程序首次运行大约一分钟后,程序将报告文件系统为“只读”,并将拒绝再写入数据,直到再次运行initUSB()。无论是否将fprintf()插入文件指针,都会发生这种情况。作为一个临时解决方案,我让writeEssentials()在驱动器变成只读时重新安

我的程序有一个循环,它运行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;
}
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");
}
int initUSB(){
int i;
FILE*filecheck=fopen(HMICheckfile,“r”);
对于(i=0;i<26;i++){
char usbmountfrompathry[256];
char-sdanum[5];
strcpy(usbMountFromPath,usbMountFromPath);
sprintf(sdanum,“%c1”,i+'a');
strcat(usbmountfrompatry,sdanum);
if(!mount(usbMountFromPathTry、usbmountPath、“vfat”、(long)NULL、NULL)){
printf(“装载成功\n”);
返回1;
}如果(!mount(usbmountfrompathry,usbmountpath,“vfat”,MS_REMOUNT,NULL))则为else{
printf(“装载成功\n”);
返回1;
}
printf(“装载错误:”);
printf(“%s\n”,usbMountFromPathTry);
}
printf(“装载错误\n”);
返回0;
}
writeEssentials():

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;
}
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");
}
void writeEssentials(){
FILE*FILE=fopen(usbMountEssentials,“a+”);
fflush(文件);
perror(“文件”);
if(file==NULL){
initUSB();
printf(“空文件\n”);
返回;
}
fprintf(文件“\n%s”,getDate());
fprintf(文件“%s”,getTime());
fprintf(文件“%1.2f”,getSpeed());
fprintf(文件“%d”,getRPM());
fprintf(文件“%d”,getRegen());
fprintf(文件“%d”,getAirgap());
fprintf(文件“%d”,getBattery());
fprintf(文件“%.2f”,*(getADCTemps()+计算机盒临时索引));
fprintf(文件“%.2f”,*(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*0));
fprintf(文件“%.2f”,*(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*1));
fprintf(文件“%.2f”,*(getBMS()+BMS_TEMP_INDEX+(BMS_NUM_VAR*2));
fprintf(文件“%.2f”,*(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*0));
fprintf(文件“%.2f”,*(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*1));
fprintf(文件“%.2f”,*(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*2));
fprintf(文件“%.2f”,*(getMPPT()+MPPT_TEMP_INDEX+(MPPT_NUM_VAR*3));
fprintf(文件“%s”,getLat());
fprintf(文件“%s”,getLong());
int i;
对于(i=0;i
检查
dmesg
。文件系统自动变为只读通常表示检测到某些损坏,因此内核将FS设置为只读以防止进一步损坏。

就是这样,它给出了错误:文件系统死机(dev sda1)群集计算错误(245!=244)文件系统已设置为只读,因此我运行了
fsck-r/dev/sda1
,它现在似乎可以工作了。非常感谢。