gpt4 book ai didi

c++ - 写入大于2Gb的文件

转载 作者:行者123 更新时间:2023-12-03 08:51:58 25 4
gpt4 key购买 nike

我写了一个文件,存储了从两个外部传感器读取的所有原始数据,我意识到当文件达到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的文件,以及如何实现。

非常感谢您的帮助。

最佳答案

当您声明使用linux时:man 2打开:

O_LARGEFILE
(LFS)允许文件大小不能用off_t表示(但可以表示)的文件
在off64_t中)被打开。必须定义_LARGEFILE64_SOURCE宏(之前
(包括所有头文件)以获取此定义。设置_FILE_OFF-
最好将SET_BITS功能测试宏设置为64(而不是使用O_LARGEFILE)
在32位系统上访问大文件的方法(请参见feature_test_macros(7))。

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

关于c++ - 写入大于2Gb的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com