[問題] C當中資料結構與fscanf的問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC 2013 - console
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
沒有
問題(Question):
請問一下, 我有一個結構長這樣
typedef struct
{
//int int_a;
char char_a;
char char_b;
char char_c;
char char_d;
int int_a;
}TEST_TYPE;
有一個對應的純文字檔, 內容是
1,2,3,-12345,4
利用下面的fscanf格式讀取的時候會發生int_a = -16777216 的錯誤
fscanf( opfile, "%hhu,%hhu,%hhu,%d,%hhu",
&test_data.char_a,
&test_data.char_b,
&test_data.char_c,
&test_data.int_a,
&test_data.char_d
);
但是只要把結構做一些調整, 變成
typedef struct
{
int int_a;
char char_a;
char char_b;
char char_c;
char char_d;
//int int_a;
}TEST_TYPE;
就能夠正確的讀出五個值
對於struct來說, 上下兩種排列都沒有4byte alignment的問題
即便有, 也只是會有padding而已, 不致於造成格式讀取錯誤
請問這個現象是哪裡有問題呢?
餵入的資料(Input):
1,2,3,-12345,4
預期的正確結果(Expected Output):
char_a = 1, char_b = 2, char_c = 3, char_d = 4
int_a = -12345
錯誤結果(Wrong Output):
char_a = 1, char_b = 2, char_c = 3, char_d = 4
int_a = -16777216
程式碼(Code):(請善用置底文網頁, 記得排版)
void TestFunction()
{
typedef struct
{
int int_a;
char char_a;
char char_b;
char char_c;
char char_d;
//int int_a;
}TEST_TYPE;
TEST_TYPE test_data = {0};
FILE* opfile = 0;
unsigned int index_current = 0;
opfile = fopen( "test.txt", "r" );
fscanf( opfile, "%hhu,%hhu,%hhu,%d,%hhu",
&test_data.char_a,
&test_data.char_b,
&test_data.char_c,
&test_data.int_a,
&test_data.char_d
);
fclose(opfile);
}
補充說明(Supplement):
感謝~~
update---
問題應該是因為Windows下不支援C99中hh的prefix
所以hhu會解讀成hu
在fscanf 的時候用了h的short而不是hh的char
把後面那個byte 也抓進來造成錯誤
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.87.77
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1473496950.A.E2D.html
→
09/10 17:36, , 1F
09/10 17:36, 1F
推
09/10 17:44, , 2F
09/10 17:44, 2F
感謝回應 我把解答放上來嘍~~
※ 編輯: GelionLin (1.164.126.82), 09/10/2016 18:45:15
→
09/10 19:37, , 3F
09/10 19:37, 3F
→
09/10 19:37, , 4F
09/10 19:37, 4F
→
09/10 19:38, , 5F
09/10 19:38, 5F
→
09/10 19:39, , 6F
09/10 19:39, 6F
推
09/10 21:07, , 7F
09/10 21:07, 7F
恩...是VC的問題
btw 補充一下這邊有一個列表
http://www.cplusplus.com/reference/cstdio/fscanf/
specifiers 那邊有說明
Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99.
對於VC上的這個問題 目前的解法是用short/int去接
之後再自己cast到char/unsigned char
不知道大家有沒有更好的解法?
※ 編輯: GelionLin (1.164.126.82), 09/10/2016 22:50:14
→
09/10 23:04, , 8F
09/10 23:04, 8F
→
09/10 23:05, , 9F
09/10 23:05, 9F
→
09/26 23:05, , 10F
09/26 23:05, 10F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章