2016-01-20 24 views
-1

有一個名爲employes.dat文件,其中包含歸類爲這樣的數據:提取文件數據至4個單獨表

Tremblay Alain   A 35.0 35.5 
Vachon Jean    P 40.0 22.75 
Lapalme Justin   O 40.0 15.75 
Deschenes Sylvie   P 35.0 25.0 
Lachance Carl    O 37.5 18.0 
Labonte Chantal   P 40.0 20.0 
Doucet Michel    A 40.0 33.75 

我想把名字,信,數字1和數字2到4個單獨的表(char names[]char letter[]float number1[]float number2[])。

我嘗試使用此代碼:

#include <stdio.h> 



int main() 
{ 

FILE *entree; 

char nom[750], poste[30]; 
int i = 0; 
float nbHeure[30], taux[30]; 

if(entree =fopen("employes.dat", "r")) { 
    fscanf(entree, "%30c %1c %4f %5f", &nom[i], &poste[i], &nbHeure[i], &taux[i]); 
    while(!feof(entree)) 
     fscanf(entree, "%30c %1c %4f %5f", &nom[i+1], &poste[i+1], &nbHeure[i+1], &taux[i+1]); 
    fclose(entree); 
} 

else printf("Impossible d'ouvrir le fichier!"); 

printf("%c", nom[1]); 

    return 0; 
} 

,但它不工作...有人可以幫我嗎?它是一個家庭作業,我只是開始用C語言編程。不要打擾名稱,法語fyi。

+1

不應該增加'我'嗎? – Mohammad

+0

即時編寫[i + 1]。 –

+0

'%c'格式用於單個字母。對於字符串,即char數組,使用'%s'。您可以指定格式中的最大長度,但'scanf'系列函數要求這比緩衝區大小小1。 –

回答

1

修復樣品這樣

#include <stdio.h> 

int main(void){ 
    FILE *entree; 

    char nom[30][25+1] ={{0}}, poste[30]; 
    float nbHeure[30], taux[30]; 
    int i = 0; 

    if(entree = fopen("employes.dat", "r")) { 
     while(i < 30 && 4==fscanf(entree, " %25c %c %f %f", nom[i], &poste[i], &nbHeure[i], &taux[i])){ 
      ++i; 
     } 
     fclose(entree); 

     int n = i; 
     for(i = 0; i < n; ++i) 
      printf("%s, %c, %.1f, %.2f\n", nom[i], poste[i], nbHeure[i], taux[i]); 
    } 
    else 
     printf("Impossible d'ouvrir le fichier!"); 

    return 0; 
} 

端修剪版本。

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

char *trimEnd(char *str){ 
    if(!str || !*str) 
     return str; 
    char *endp = strchr(str, '\0'); 
    while(isspace(*--endp)){ 
     *endp = 0; 
    } 
    return str; 
} 

int main(void){ 
    FILE *entree; 

    char nom[30][25+1] ={{0}}, poste[30]; 
    float nbHeure[30], taux[30]; 
    int i = 0; 

    if(entree = fopen("employes.dat", "r")) { 
     while(i < 30 && 4==fscanf(entree, " %25c %c %f %f", nom[i], &poste[i], &nbHeure[i], &taux[i])){ 
      ++i; 
     } 
     fclose(entree); 

     int n = i; 
     for(i = 0; i < n; ++i) 
      printf("%s, %c, %.1f, %.2f\n", trimEnd(nom[i]), poste[i], nbHeure[i], taux[i]); 
    } 
    else 
     printf("Impossible d'ouvrir le fichier!"); 

    return 0; 
} 
+0

非常感謝你Bluepixy –

0

我有固定的代碼最小的變化,所以你不會失去你最初的想法,我會解釋我做了什麼:

#include <stdio.h> 

int main() 
{ 

FILE *entree; 

char nom[750], poste[30]; 
int i = 0; 
int j = 0; 
int k = 0; 
float nbHeure[30], taux[30]; 

if(entree =fopen("employes.dat", "r")) { 
    fscanf(entree, "%25c %1c %4f %5f", &nom[j], &poste[i], &nbHeure[i], &taux[i]); 
    while(!feof(entree)) { 

     j = j + 25; 
     i = i + 1; 
     fscanf(entree, "%25c %1c %4f %5f", &nom[j], &poste[i], &nbHeure[i], &taux[i]); 

     if (!feof(entree)) { 

     } else { 
      j = j - 25; 
      i = i - 1; 
     } 

    } 
    fclose(entree); 
} 

else printf("Impossible d'ouvrir le fichier!"); 

    printf ("%s \n \n", nom); 
    printf ("%s \n \n", poste); 

    for (k = 0; k < i; k++) { 
     printf ("%f ", nbHeure[k]); 
    } 

    printf("\n"); 
    for (k = 0; k < i; k++) { 
     printf ("%f ", taux[k]); 
    } 

    return 0; 
} 

我宣佈另一個整數,J

int j = 0; 

因爲i整數將指向數組中的位置,其中一個元素的大小爲1,並且j整數將指向數組中每個元素具有更多大小的位置的位置一個(在我們的例子中,我計算,每個名稱25)。讓我通過示例來解釋:

如果j現在爲0,則我們在nom數組中寫入的下一個名稱將從位置0開始。正確。

j = 0; fscanf(... %25c ... &nom[j]...); 

這意味着從位置j = 0到位置j的所有字符= 24(=總共25個字符)將充滿了從文件中的第一個名字。好

我們想寫的下一個名字應該從前一個名字的結尾位置開始。完全是25。爲此,我們將j值增加25 j = j + 25; 現在下一個單詞將從j + 25開始寫入到j + 49。等等。你會發現我做

一件事就是補充一點:

if (!feof(entree)) { 

    } else { 
     j = j - 25; 
     i = i - 1; 
    } 

從文件中讀取數據時,你問頂部是該文件還沒有結束?(feof)。如果不是,請繼續閱讀。但是,只有從文件中讀取特殊的EOF字符後,文件纔會結束。當你閱讀它時,你也會嘗試寫出名字和稅號,並且這也不是我們想要的。如果我寫的只是避免從文件中讀取最後一行,這類似於我告訴過你的EOF。

最後,我爲您的陣列添加了一些打印,以便您可以看到結果。

希望這有助於。

相關問題