Re: [問題] fopen() & fwrite() 問題
※ 引述《KSmart (經濟不景氣,好景不常在)》之銘言:
: 2.fwrite的問題
: 目前有個字串,假設為
: string hextemp="1F 08 08 0A 3A 5C 2E 6F 05"
: 如果我要以16進制存成.JPG檔,那麼我需要去處理字串中間的空格嗎??
: 還是只需要把字串直接用fwrite寫成檔案就可以了呢??
: 目前一直卡在這邊,腦袋一直轉不過來...
: 我想到的是,先把以上字串存入字元陣列,再用strtok去除中間空格..
: 然後再把每一個16進位寫到另一個陣列中
: char temp[]={1F,08,08,0A,3A,5C,2E,6F,05}
: fwrite(temp, sizeof(temp), 1, fw)
: 然後再寫入檔案,不曉得是不是我搞錯方向了?其實不用那麼麻煩?
第一個問題已經解決了,但是第二個問題一直卡住...
我要的檔案應該是用UltraEdit開啟會像 http://ppt.cc/0m5n
我使用推文中的linotwo的方式去以hex讀取字串..
但是寫到檔案中時,似乎跟連結中的形式不同...
請問一下,我是不是還要再做一次hex->ASCII string的動作?
如果用istringstream的話,可以直接以ASCII string的方式讀取嗎??
int nBodyLength =440000;//假設為440000
string strDate="20121015"
string strBody="1F 08 08 0A 3A 5C..."//實際上很長,大概有40萬個16進位
string pathtemp="C:\\Temp\\"+strDate;
FILE* fw=fopen(pathtemp.c_str(),"wb");
if (fw!=NULL)
{
istringstream iss(strBody, istringstream::in);
iss >> hex;
int val;
unsigned char *buffer = (unsigned char *)calloc
(nBodyLength,sizeof(unsigned char));
while(!iss.eof())
{
size_t byteCount = 0;
while(byteCount < sizeof(buffer) && iss >> val)
{
//想說在這邊直接用char(toascii(val))轉成ASCII再存入buffer
//但是不管怎麼轉,寫入的檔案都失敗..無法像連結圖片那樣..
buffer[byteCount] =val;
++byteCount;
}
fwrite(&buffer, sizeof(buffer[0]), byteCount, fw);
}
free(buffer);
fclose(fw);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.171.31.44
※ 編輯: KSmart 來自: 118.171.31.44 (10/15 23:48)
→
10/16 19:07, , 1F
10/16 19:07, 1F
→
10/16 19:09, , 2F
10/16 19:09, 2F
→
10/16 19:11, , 3F
10/16 19:11, 3F
→
10/16 19:12, , 4F
10/16 19:12, 4F
→
10/16 19:15, , 5F
10/16 19:15, 5F
→
10/20 01:05, , 6F
10/20 01:05, 6F
討論串 (同標題文章)