2016-01-10 60 views
1

我必須使用此結構讀取文本文件。另外,我必須使用外部功能。我做了文件讀取的代碼,它在主函數中工作。作爲extern函數的參數的結構C

文本文件:

banana 3 orange 8 music 9 - 第一個字符是一個空格*

#include <stdio.h> 
#include <stdlib.h> 

struct file 
{ 
    char name[30]; 
    char size; 
}; 


int main() 
{ 
    int n=0; 
    struct file f[30]; 
    FILE *files; 
    files=fopen("files.txt","r"); 
    int n=0; 
    while (1) 
    { 
     fgetc(files); 
     if(feof(files)) break; 
     fscanf(files,"%s %c",&f[n].name,&f[n].size); 
     n++; 
    } 
} 

但是當我嘗試使用其他C文件和外部函數它沒有工作,使這個閱讀.. :(

這是寫在filereading.c:

void fileReading(struct file *f[30], FILE *files) 
{ 
    int n=0; 
while (1) 
{ 
    fgetc(files); 
    if(feof(files)) break; 
    fscanf(files,"%s %c",&f[n].name,&f[n].size); 
    n++; 
} 
} 

而fileReading.h:

void fileReading(struct fisier *, FILE *); 

而且main.c中:

#include <stdio.h> 
#include <stdlib.h> 

struct file 
{ 
    char name[30]; 
    char size; 
}; 


int main() 
{ 
    int n=0; 
    struct file f[30]; 
    FILE *files; 
    files=fopen("files.txt","r"); 
    fileReading(f[30],files); 
} 

當我編譯它,它說:

request for member 'name' in something not a structure or union 
request for member 'size' in something not a structure or union| 
||=== Build finished: 2 errors, 2 warnings (0 minutes, 0 seconds) ===|| 

你能幫助我嗎?謝謝!

+0

'無效fileReading(結構文件* F [30],FILE *文件)' - >'無效fileReading(當我在''fileReading.h''中包含'fileReading.h'時,'fileReading(f,files);' – BLUEPIXY

回答

2

從我看來,它看起來像你沒有很好的理解指針。 這些變化應該解決您的問題:

void fileReading(struct file *f, FILE *files) 
{ 
    int n=0; 
while (1) 
{ 
    fgetc(files); 
    if(feof(files)) break; 
    fscanf(files,"%s %c",f[n].name,&f[n].size); 
    //printf("%s %c",f[n].name,f[n].size); 
    n++; 
} 
} 
int main() 
{ 
    int n=0; 
    struct file f[30]; 
    FILE *files; 
    files=fopen("files.txt","r"); 
    fileReading(f,files); 
} 

你做了什麼錯:

void fileReading(struct file *f[30], FILE *files) //here you were saying file is a ** 
fscanf(files,"%s %c",&f[n].name,&f[n].size); // here you need to send the a char* but you were sending a char ** as a second parameter 
fileReading(f[30],files); // here you were sending the 31th element of the structure array f which by the way doesn't exist (indexing is from 0 , f[29] is the last) even though that was not what you wanted to do in the first place 
1

文件fileReading.c不知道struct file的定義。你需要在main.c和fileReading.c中將它從main.c移動到fileReading.h和#include "fileReading.h"

此外,fileReading的定義和調用不正確。相反的:

void fileReading(struct file *f[30], FILE *files) 

你想:

void fileReading(struct file *f, FILE *files) 

你這樣稱呼它:

fileReading(f,files); 

這是不正確的:

fileReading(f[30],files); 

因爲你傳遞一個struct file而不是數組,和您傳遞的單個實例是數組末尾的一個元素(因爲大小爲30,有效索引爲0-29),這可能導致不確定的行爲。

+0

' main.c'和'fileReading.c'它說: 'fisiere.h:沒有這樣的文件或目錄| || ===構建完成:1個錯誤,0個警告(0分鐘,0秒)=== |' – ProgrammingFailure

+0

@ProgrammingFailure那麼你的頭文件是fileReading.h還是fisiere.h?不管它叫什麼,確保該文件與.c文件位於同一目錄中,並且文件的真實名稱包含在兩個源文件中。 – dbush

+0

@ProgrammingFailure在發佈代碼時,通過複製/粘貼發佈* actual *代碼,而不是單獨鍵入或進行額外編輯。 – dbush