2013-01-20 186 views
-3

我想在C編程用戶定義的函數,其中該函數將返回從文件中的文本和文件名通過參數該函數傳遞給函數。 謝謝。一個函數讀取文本文件

這是因爲我需要的文本到一個變量追加。我如何修改下面的代碼。這是我試圖做到這一點,但我現在得到很多錯誤。

example1.c: In function ‘readfile’: 
example1.c:47:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default] 
/usr/include/stdio.h:273:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 
example1.c:48:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration] 
example1.c:48:52: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:56:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] 
example1.c:56:22: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] 
example1.c:57:57: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:61:59: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:65:2: warning: return makes integer from pointer without a cast [enabled by default] 
example1.c:68:5: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] 
example1.c:68:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default] 
example1.c: At top level: 
example1.c:71:30: warning: initialization makes pointer from integer without a cast [enabled by default] 
example1.c:71:1: error: initializer element is not constant 

請幫忙。

char readfile (fname) { 
    FILE * pFile; 
    long lSize; 
    char * buffer; 
    size_t result; 

    pFile = fopen (fname , "rb"); 
    if (pFile==NULL) {fputs ("File error",stderr); exit (1);} 

    fseek (pFile , 0 , SEEK_END); 
    lSize = ftell (pFile); 
    rewind (pFile); 

    buffer = (char*) malloc (sizeof(char)*lSize); 
    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} 

    result = fread (buffer,1,lSize,pFile); 
    if (result != lSize) {fputs ("Reading error",stderr); exit (3);} 

    return buffer; 
    fclose (pFile); 
    free (buffer); 
} 
AC_ALPHABET_t * input_text = readfile("infile"); 
+1

您可能需要一位編輯來完成此操作。和兩隻手(或至少一隻手) – wildplasser

+0

你自己回答了你的問題。將文件名作爲參數傳遞並返回文本。其餘的是「你的」代碼... – EarlOfEgo

+0

@EarlOfEgo 這個函數的返回類型爲int是確定的,如果我使用CHAR – rkrara

回答

1

這確實應該是很容易與一些研究的事,但我已經修改了你的程序,以便它有一個返回給定文件名的字符串的函數。我想這不是最簡單,最好或最正確的方法,但你要求修改你的程序。

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

char *read_from_file(const char *filename) 
{ 
    long int size = 0; 
    FILE *file = fopen(filename, "r"); 

    if(!file) { 
     fputs("File error.\n", stderr); 
     return NULL; 
    } 

    fseek(file, 0, SEEK_END); 
    size = ftell(file); 
    rewind(file); 

    char *result = (char *) malloc(size); 
    if(!result) { 
     fputs("Memory error.\n", stderr); 
     return NULL; 
    } 

    if(fread(result, 1, size, file) != size) { 
     fputs("Read error.\n", stderr); 
     return NULL; 
    } 

    fclose(file); 
    return result; 
} 

int main(int argc, char **argv) 
{ 
    if(argc < 2) { 
     fputs("Need an argument.\n", stderr); 
     return -1; 
    } 

    char *result = read_from_file(argv[1]); 

    if(!result) return -1; 

    fputs(result, stdout); 
    free(result); 

    return 0; 
}    

採樣運行:

[[email protected] ~]$ echo "hello world" > some_file.txt 
[[email protected] ~]$ ./test some_file.txt 
hello world 
[[email protected] ~]$ 

我可以在必要時發表意見的程序,但查找函數的引用可能是不夠的。

+0

非常感謝,我會嘗試這一點,讓知道。 – rkrara

+0

感謝一些修改,以適合我的代碼後,像這樣 example1.c一些警告工作:在函數「read_from_file」: example1.c:88:5:警告:函數「的malloc」隱式聲明[-Wimplicit功能-declaration] example1.c:88:29:警告:默認情況下啓用] – rkrara

+0

@ user1733911,內置功能「的malloc」不兼容的隱式聲明你包括頭?它在那裏定義。 –