今天跟 外國學長討論時發現的,
主要是 ftell 這個函數的問題,
當使用 "r" 是文字讀取模式,
這個模式下 ftell 常常會得到比預期還長的長度,
也就是說 intFileEndByte = ftell(bFile); 得到的值會是錯的,
解決方法是 "r" 改成 "rb" ,這個問題真的很鳥。
fread進來的東西有時還會給你最後兩行複製一次再加一堆亂碼。
程式碼如下:
 
// test_ftell.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
FILE *bFile;
FILE *bFileOut;
bFileOut = fopen("Out.txt","w");
bFile = fopen("cameras.txt","r");
int intFileEndByte;
fseek(bFile,(int) 0,SEEK_END);
intFileEndByte = ftell(bFile);
fseek(bFile, 0, SEEK_SET);
char * buffer;

buffer = new char[intFileEndByte+1];
fread_s(buffer, intFileEndByte+1, 1, intFileEndByte, bFile);
buffer[intFileEndByte] = '\n';
// get by line
char* pToken = 0;
pToken = buffer;
float data[11];
char imgPath [256];
fwrite(buffer, intFileEndByte, 1, bFileOut);
fclose(bFile);
fclose(bFileOut);
}
arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()