標(biāo)準(zhǔn)版U盤文件讀寫模塊,并口查詢連接,單片機(jī)是內(nèi)部帶1024字節(jié)RAM的,在原文件后面追加同樣的數(shù)據(jù)結(jié)果怎么老是亂碼啊~~~~~
printf( "Open\n" ); memcpy( mCmdParam.Open.mPathName, "\\測(cè)量數(shù)據(jù).TXT", MAX_PATH_LEN ); i = ExecCommand( CMD_FileOpen, MAX_PATH_LEN ); if ( i == ERR_MISS_FILE ) { printf( "Create\n" ); memcpy( mCmdParam.Create.mPathName, "\\測(cè)量數(shù)據(jù).TXT", MAX_PATH_LEN ); i = ExecCommand( CMD_FileCreate, MAX_PATH_LEN ); mStopIfError( i ); NewSize = 0; } else { /* 找到文件\測(cè)量數(shù)據(jù).TXT或者出錯(cuò) */ mStopIfError( i ); printf( "Query\n" ); i = ExecCommand( CMD_FileQuery, 0 ); /* 查詢當(dāng)前文件的信息,沒(méi)有輸入?yún)?shù) */ mStopIfError( i ); OldSize = mCmdParam.Modify.mFileSize; /* 原文件的長(zhǎng)度 */ SecCount = ( OldSize + 511 ) >> 9; /* (OldSize+511)/512, 計(jì)算文件的扇區(qū)數(shù),因?yàn)樽x寫是以扇區(qū)為單位的 */ NewSize = (unsigned short)OldSize; /* 原長(zhǎng)度 */ printf( "Size=%ld, Len=%d, Sec=%d\n", OldSize, NewSize, (unsigned short)SecCount );
mCmdParam.Locate.mSectorOffset = 0xFFFFFFFF; i = ExecCommand( CMD_FileLocate, 4 ); mStopIfError( i ); } printf( "Write\n" ); strcpy( DATA_BUF, "abcdef\xd\n"); mCmdParam.Write.mSectorCount = 0x01; /* 寫入一個(gè)扇區(qū)*/ i = ExecCommandBuf( CMD_FileWrite, 1, DATA_BUF ); /* 向文件寫入數(shù)據(jù) */ /*printf("%d\n",mCmdParam.Write.mSectorCount);*/ mStopIfError( i );
NewSize = NewSize + strlen( DATA_BUF ); /* 新文件的長(zhǎng)度 */
printf( "Modify\n" ); mCmdParam.Modify.mFileAttr = 0xff; /* 輸入?yún)?shù): 新的文件屬性,為0FFH則不修改 */ mCmdParam.Modify.mFileTime = 0xffff; mCmdParam.Modify.mFileDate = ( (2004-1980)<<9 ) + ( 5<<5 ) + 18; /* 輸入?yún)?shù): 新的文件日期: 2004.05.18 */ mCmdParam.Modify.mFileSize = NewSize; /* 輸入?yún)?shù): 如果原文件較小,那么新的文件長(zhǎng)度與原文件一樣長(zhǎng),否則被RAM所限 */ i = ExecCommand( CMD_FileModify, 4+2+2+1 ); mStopIfError( i );
mDelaymS( 500 ); printf( "Close\n" ); mCmdParam.Close.mUpdateLen = 0; /* 不要自動(dòng)計(jì)算文件長(zhǎng)度,如果自動(dòng)計(jì)算,那么該長(zhǎng)度總是512的倍數(shù) */ i = ExecCommand( CMD_FileClose, 1 ); mStopIfError( i );
第一次寫的數(shù)據(jù)是正確的,但是第二次往后面追加的數(shù)據(jù)就變成亂碼了,結(jié)果是這樣: abcdef o<可Y# 修改文件長(zhǎng)度應(yīng)該不會(huì)有問(wèn)題吧,請(qǐng)達(dá)人指點(diǎn)??!