写一个大于2Gb的文件

时间:2016-06-30 07:45:24

标签: c++ file error-handling raspbian

我写了一个文件,存储了我从两个外部传感器读取的所有原始数据,并且我意识到当文件获得2Gb时程序停止写入数据,程序连续工作,但它不会在文件中写入更多内容。

我正在使用下一个代码(它是一个等待信号并在文件中写入的线程):

while(1)
{
    //Lock, Wait the signal and unlock
    pthread_mutex_lock(&mutex_WriteOFIMUandGPS);
    pthread_cond_wait(&cond_WriteOFIMUandGPS, &mutex_WriteOFIMUandGPS);
    X_CopySnapshot = X_Snapshot;
    Y_CopySnapshot = Y_Snapshot;
    Z_CopySnapshot = Z_Snapshot;
    Vx_CopySnapshot = Vx_Snapshot;
    Vy_CopySnapshot = Vy_Snapshot;
    Vz_CopySnapshot = Vz_Snapshot;
    HPL_CopySnapshot = HPL_Snapshot;
    HDOP_CopySnapshot = HDOP_Snapshot;
    StdPosX_CopySnapshot = StdPosX_Snapshot;
    StdPosY_CopySnapshot = StdPosY_Snapshot;
    StdPosZ_CopySnapshot = StdPosZ_Snapshot;
    pthread_mutex_unlock(&mutex_WriteOFIMUandGPS);


    //Get Time and Date
    now = time(0);
    localtm = localtime(&now);

    //Get millis
    gettimeofday(&tp, NULL);
    ms = round(tp.tv_usec / 1000);
    sprintf(buf,"%03d",(int)ms);

    //Empty strings
    DateTime = "";
    text.clear();
    text.str(string());

    //Store Data and Time information in a string
    text << localtm->tm_year+1900 << "/";
    text << (((localtm->tm_mon+1)<10) ? "0" : "") << localtm->tm_mon+1 << "/";
    text << ((localtm->tm_mday<10)? "0" : "") << localtm->tm_mday << ",";
    text << ((localtm->tm_hour<10)? "0" : "") << localtm->tm_hour << ":";
    text << ((localtm->tm_min<10)? "0" : "") << localtm->tm_min << ":";
    text << ((localtm->tm_sec<10)? "0" : "") << localtm->tm_sec << "."<< buf;
    DateTime = text.str();

    //Save data
    fprintf(fid,"%s,"
            "2,6,0,"                            // Alg_ID,SolStatus,EGNOSStatus,
            "%12.3f,%12.3f,%12.3f,"             // XyzUKF[0],XyzUKF[1],XyzUKF[2],
            "%8.8f,%8.8f,%8.8f,%7.18f,"         // V_UKF[0],V_UKF[1],V_UKF[2],HPL,
            "0,0,%5.2f,nan,nan,nan,nan,"        // NumSat,NumSatEx,HDOP,PDC1,PDC2,PDC3,PDC4,
            "%7.2f,%7.2f,%7.2f,"                // StdPos[0],StdPos[1],StdPos[2]
            "0,0,0,"                            // StdVel[0],StdVel[1],StdVel[2],
            "1,0,"                              // TypePositioning[0],TechUsedPos,
            "0,0,0,0,0,"                        // Observables
            "0,0,0,0,0,"                        // Observables
            "0,0,"                              // VPL,VDOP
            "0,"                                // TechRec
            "0,0,0,0,0,"                        // Observables
            "0,0,0,0,0\n",                      // Observables
            DateTime.c_str(),
            X_CopySnapshot, Y_CopySnapshot, Z_CopySnapshot,
            Vx_CopySnapshot, Vy_CopySnapshot, Vz_CopySnapshot,HPL_CopySnapshot,
            HDOP_CopySnapshot,
            StdPosX_CopySnapshot, StdPosY_CopySnapshot, StdPosZ_CopySnapshot);
}

我不知道是否有办法编写大于2Gb的文件以及我应该如何实现它。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

当你声明你使用linux:man 2 open:

       O_LARGEFILE
              (LFS) Allow files whose sizes cannot be represented in an off_t (but can be represented
              in  an  off64_t)  to  be opened.  The _LARGEFILE64_SOURCE macro must be defined (before
              including any header files) in order to obtain this definition.  Setting the _FILE_OFF-
              SET_BITS  feature  test  macro  to  64 (rather than using O_LARGEFILE) is the preferred
              method of accessing large files on 32-bit systems (see feature_test_macros(7)).

否则,文件操作仅限于2GiB。