作者GelionLin (蓋立安)
看板C_and_CPP
標題[問題] C當中資料結構與fscanf的問題
時間Sat Sep 10 16:42:28 2016
開發平台(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
→ Caesar08: 我猜你文字檔內容跟你打的不一樣09/10 17:36
推 yvb: 是否有 #include <stdio.h> ? 或看一下編譯時有沒有什麼訊息?09/10 17:44
感謝回應 我把解答放上來嘍~~
※ 編輯: GelionLin (1.164.126.82), 09/10/2016 18:45:15
→ hunandy14: 這好奇怪呀,跟windwos會有關呀,有差的不是 09/10 19:37
→ hunandy14: 編譯器差異或是版本差異嗎~ 09/10 19:37
→ hunandy14: win的 gcc 跟linux 的gcc 會不一樣嗎@@ 09/10 19:38
→ hunandy14: 我有漏了些什麼嗎~純推理去想的 09/10 19:39
推 yvb: 因為漏看了文章開頭 ==> 開發平台 VC 2013 09/10 21:07
恩...是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
→ Caesar08: 換compiler 09/10 23:04
→ Caesar08: 既然用visual studio,何不寫C++就好了?為甚麼要用C? 09/10 23:05
→ GelionLin: VC++只是作為IDE, 目標是嵌入式平台 09/26 23:05