2013-05-12 62 views
0

我試圖掃描一個包含13個要存儲在13個變量中的整數的文件。有沒有辦法循環這一點,而跳過第i個元素?我期待有可能是一個解決方案,這還沒有躲避我,也許類似下面的代碼:c:用scanf跳過一個元素

int i; 
for (i = 0; i < 13; i++) 
    fscanf(file, %d, &variables[i]); // somehow apply i to %d 

,而不是很明顯,但冗長的和不潔的:

fscanf(file, %d, &variable1); 
fscanf(file, %*d, %d, &variable2); 
fscanf(file, %*d %*d, %d, &variable3); // etc 

感謝

+0

您想要讀取元素,跳過1元素,讀取元素,跳過2個元素,讀取元素,跳過3個元素等等? – Barmar 2013-05-12 11:23:04

+0

是的,在變量x中存儲元素x :) – kensing 2013-05-12 11:44:10

+0

這不是一回事。您想要將變量[0]中的元素0,變量[1]中的元素2,變量[2]中的元素5等等存儲起來? – Barmar 2013-05-12 11:59:25

回答

1
int *variables[] = { &variable1, &variable2, &variable3, ... }; 

for (int i = 0; i < 13; i++) { 
    fscanf(file, "%d", variables[i]); 
}