Executing sync() on Linux will lose files

Viewed 29

I have a program that converts filename.json to filename.json.lua, and then executes a sync (). A problem is found. When my filename.json is larger than 1M, filename.json.lua cannot be generated. What is the reason?

My code is code 1 and code 2, and code1 calls code 2.

filename.json:

    [
        {
            "armOrientation":[-1,-1,-1,0],
            "alias":"",
            "coordinate":[-489.4559,-87.2307,372.8333,-168.3145,8.2814,64.4153],
            "joint":[-152.4393,-0.422,-110.3601,34.3767,85.5572,53.4645],
            "name":"P1","tool":0,"user":0
        },
        {
            "armOrientation":[-1,-1,-1,0],
            "alias":"",
            "coordinate":[-481.7919,-122.6899,372.8333,-168.3145,8.2814,68.5971],
            "joint":[-148.2575,-0.422,-110.3601,34.3767,85.5572,53.4645],
            "name":"P2","tool":0,"user":0
        }
           ....
           ....
           ....
   ]  

filename.json.lua:

    P1={coordinate={-489.455902, -87.230698, 372.833313, -168.314499, 8.281400, 64.415298}, 
    tool=0, user=0,joint={-152.439301, -0.422000, -110.360100, 34.376701, 85.557198, 53.464500},
    armOrientation={-1,-1,-1,0}}
    P2={coordinate={-481.791901, -122.689903, 372.833313, -168.314499, 8.281400, 68.597099}, 
    tool=0, user=0,joint={-148.257507, -0.422000, -110.360100, 34.376701, 85.557198, 53.464500},
    armOrientation={-1,-1,-1,0}}

code 1: (Just pseudo code)

    {
        JsonToLua("filename.json");
        sync()
    }

 

code 2:

    bool JsonProjectReader::JsonToLua(const char *path)
    {
        static char jsonPath[200];
        static char luaPath[200];
    
        memset(jsonPath, 0, sizeof(jsonPath));
        memset(luaPath, 0, sizeof(luaPath));
    
        snprintf(jsonPath, sizeof(jsonPath), "%s/%s", PRP_PROJECT, path);
        if(access(jsonPath, F_OK) == -1){
            SPDLOG_ERR(channel_http, "Can't find file:{}", jsonPath);
        }
        snprintf(luaPath, sizeof(luaPath), "%s.lua", jsonPath);
    
        Json::Value root;
        READER_FILE(root, jsonPath);
    
        FILE *fp;
        if((fp = fopen(luaPath, "w")) == NULL){
            SPDLOG_ERR(channel_http, "Create file lua error {}", luaPath);
        }else{
            SPDLOG_INFO(channel_http,"Create file lua {}", luaPath);
            for(Json::ArrayIndex i = 0; i < root.size(); i++){
                Json::Value name = getVal(&root[i], "name");            
                Json::Value coordinate = getVal(&root[i], "coordinate");
                Json::Value tool = getVal(&root[i], "tool");
                Json::Value user = getVal(&root[i], "user");            
                fprintf(fp, "%s={coordinate={%f, %f, %f, %f, %f, %f}, tool=%d, user=%d"
                    ,name.asString().data()
                    ,coordinate[0].asFloat(),coordinate[1].asFloat(),coordinate[2].asFloat()
                    ,coordinate[3].asFloat(),coordinate[4].asFloat(),coordinate[5].asFloat()
                    ,tool.asInt(), user.asInt());
                if (!isEmptyItem(&root[i], "load")){
                    Json::Value load = getVal(&root[i], "load");
                    fprintf(fp, ",load=%f",load.asFloat());
                }
                if (!isEmptyItem(&root[i], "joint")){ // 2022-01-05 joint 和 armOrientation 字段 两个都要
                    Json::Value joint = getVal(&root[i], "joint");
                    if (!joint.isArray()){
                        SPDLOG_ERR(channel_http, "joint is not Array,break!!!");
                        break;
                    }
                    fprintf(fp, ",joint={%f, %f, %f, %f, %f, %f}",
                        joint[0].asFloat(),joint[1].asFloat(),joint[2].asFloat(),
                        joint[3].asFloat(),joint[4].asFloat(),joint[5].asFloat());
                        
                }
                if (!isEmptyItem(&root[i], "armOrientation")){
                    Json::Value armOrientation = getVal(&root[i], "armOrientation");
                    if (!armOrientation.isArray()){
                        SPDLOG_ERR(channel_http, "armOrientation is not Array,break!!!");
                        break;
                    }
                    fprintf(fp, ",armOrientation={%d,%d,%d,%d}",
                        armOrientation[0].asInt(),armOrientation[1].asInt(),
                        armOrientation[2].asInt(),armOrientation[3].asInt());
                }
                fprintf(fp, "}\n");
            }
            fclose(fp);
            return true;
        }
    
        return false;
    }
0 Answers
Related