2012-03-31 123 views
0
Operating System Used:Ubuntu 11.04 
Compiler Used: GCC 

計劃相關的文件:Git Hub Link無法將文件的內容複製到緩衝區

我想一個實施計劃,將做的工作,相同CPP(C預處理器)時,我的編譯一個.c文件。

在這個特殊的代碼中Copy_file_to_buf函數沒有將整個文件拷貝到緩衝區中。 acutal的大小是117406C,但在copy_file_to_buf函數中顯示它爲114689

編輯: 有當我複製的dummyfile內容使用同一程序緩衝區無數據丟失,但我已經在temp.c文件seperately書面copy_file_to_buf程序。

temp.c

#include<stdio.h> 
main(int argc,char **argv) 
{ 
    FILE *inputFile; 
    int sizeofFile, rc; 
    char *source_buf; 
    fprintf(stderr, "In Copy_file_to_buf\n"); 
    sleep(1); 

    inputFile=fopen(argv[1],"r"); 
    if (!inputFile) { 
     fprintf(stderr, "Oops, failed to open inputfile \"%s\"\n", argv[1]); 
     return NULL; 
     } 

    fseek(inputFile,0,SEEK_END); 
    sizeofFile=ftell(inputFile); 

    fseek(inputFile,0,SEEK_SET); 
    source_buf=calloc(1,1+sizeofFile); 
    rc = fread(source_buf,sizeofFile,1,inputFile); 
    /* now check rc */ 
    fprintf(stderr, "Size of the file=%d; fread returned %d.\n", sizeofFile, rc); 
    //sleep(5); 
    fclose(inputFile); 
    source_buf[sizeofFile] = 0; 
    puts(source_buf); 
    return source_buf; 
} 

貌似FSEEK(),並如預期下面的代碼FTELL不工作。

puts("Copying dummyfile contents"); 
test_buf=Copy_file_to_buf("dummyfile"); 
puts(test_buf); 

文件名:preprocessor.c

#include"myheader.h" 

/* agrv[1]=preprocessor 
* argv[2]=test.c 
* 
* Program on PREPROCESSOR 
* 
* Steps: 
* 1.Removal of comments. 
* 2.Inclusion of headerfiles. 
* 3.Macro substitution. 
*  a.function like arguments 
*  b.Stringification 
*  c.Concatenation 
* 4.Conditional compilation 
*  a.#Ifdef 
*  b.#If 
*  c.#defined 
*  d.#Else 
*  e.#Elif 

*/ 


int 
main(int argc, char *argv[]) 
{ 
    char *source_buf,*subBuf,*rmc_buf,*test_buf; 
    char **main_header_names,**sub_header_names; 
    int main_header_count,sub_header_count; 

    source_buf=(char *)Copy_file_to_buf(argv[1]);//... 
    rmc_buf=removeComments(source_buf);//... 
    main_header_names=(char **)getMainHeaderNames(rmc_buf);//... 
    source_buf=(char *)Copy_file_to_buf(argv[1]);//... 
    rmc_buf=removeComments(source_buf);//... 
    main_header_count=mainHeaderCounter(rmc_buf);//... 
    printf("Main Header Count=%d",main_header_count);//... 
    includeHeaders(main_header_names,main_header_count); 

    subBuf=(char *)Copy_file_to_buf("pre.i");//... 
    sub_header_names=(char **)getSubHeadersNames(subBuf);//... 

    subBuf=(char *)Copy_file_to_buf("pre.i");//... 
    sub_header_count=subHeadersCounter(subBuf);//... 

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile");//... 

    puts("Copying dummyfile contents"); 

    test_buf=Copy_file_to_buf("dummyfile"); 
    puts(test_buf); 

    /*test_buf=removeComments(test_buf); 
    puts(test_buf); 
    sub_header_names=(char **)getSubHeadersNames(test_buf); 
    test_buf=(char *)Copy_file_to_buf("dummyfile"); 
    sub_header_count=subHeadersCounter(test_buf); 

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile2"); 
    printf("Line:%d File:%s",__LINE__,__FILE__); 
    */ 

return 0; 
} 

文件名:CopyFile.c

#include"myheader.h" 

//Copying input file data into source_buf 

char * Copy_file_to_buf(char *fileName) 
{ 
    FILE *inputFile; 
    int sizeofFile; 
    char *source_buf; 
    puts("In Copy_file_to_buf"); 

    inputFile=fopen(fileName,"r"); 
    fseek(inputFile,0,2); 
    sizeofFile=ftell(inputFile); 
    sizeofFile++; 
    fseek(inputFile,0,0); 
    source_buf=calloc(1,sizeofFile); 
    fread(source_buf,sizeofFile,1,inputFile); 
    printf("SIZE OF THE FILE=%d",sizeofFile); 
    //sleep(5); 
    fclose(inputFile); 

    return source_buf; 
} 
+1

如果您碰巧在Microsoft平臺上,可能需要'fopen(fileName,「rb」);'。 – wildplasser 2012-03-31 12:13:22

+0

@wildplasser 我在Unbuntu 11.04上編譯了上面的代碼。 我嘗試過'fopen(fileName,「rb」);',但結果是一樣的。 – SlashQB 2012-03-31 12:16:41

+0

請使用預定義的宏('SEEK_SET','SEEK_CUR'或者'SEEK_END')來代替魔術常量。 – pmg 2012-03-31 12:21:05

回答

1

你問fread讀取一個塊sizeofFile長。如果無法讀取大小的塊,則會失敗並返回零。相反,您最好請求sizeofFile塊長度爲1個字節。然後它會報告它設法讀取的字節數量,這可能比sizeofFile更少,原因很多。

rc = fread(source_buf, 1, sizeofFile inputFile) ; 

在你的情況下,它總是會少於sizeofFile,因爲你以前增加了,所以從來沒有人要讀sizeofFile字節。您應該讀取文件大小而不是緩衝區大小,但讀取一個字節的多個塊仍然是可取的。其實我建議以下變化:

sizeofFile = ftell(inputFile) ; 
// REMOVED sizeofFile++ ; 
fseek(inputFile, 0, SEEK_SET) ; 
source_buf = calloc(1, sizeofFile + 1) ;  // Don't confuse file size and buffer size here. 
rc = fread(source_buf, 1, sizeofFile, inputFile) ; // SWAPPED param 2 and 3 

這是有道理的,如果你正在讀固定長度的記錄使用除1以外的塊大小。如果讀取任意長度的字節流,其大小通常應爲1,並使用計數來確定讀取的數據量。

1
char * Copy_file_to_buf(char *fileName) 
{ 
    FILE *inputFile; 
    int sizeofFile, rc; 
    char *source_buf; 
    fprintf(stderr, "In Copy_file_to_buf\n"); 

    inputFile=fopen(fileName,"r"); 
    if (!inputFile) { 
     fprintf(stderr, "Oops, failed to open inputfile \"%s\"\n", fileName); 
     return NULL; 
     } 

    fseek(inputFile,0,SEEK_END); 
    sizeofFile=ftell(inputFile); 

    fseek(inputFile,0,SEEK_SET); 
    source_buf=calloc(1,1+sizeofFile); 
    rc = fread(source_buf,sizeofFile,1,inputFile); 
    /* now check rc */ 
    fprintf(stderr, "SIZE OF THE FILE=%d; fread returned %d.\n", sizeofFile, rc); 
    //sleep(5); 
    fclose(inputFile); 
    source_buf[sizeofFile] = 0; 
    return source_buf; 
} 

UPDATE:測試增加主+包括...

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

int main (int argc, char **argv) 

{ 
char *lutser; 

lutser = Copy_file_to_buf(argv[1]); 

fprintf (stderr, "Strlen(lutser)=%u\n", (unsigned) strlen(lutser)); 

puts (lutser); 
return 0; 
} 

輸出:

-rw-r--r-- 1 Plasser pis 1008 2012-03-31 15:10 lutsbuf.c 
-rwxr-xr-x 1 Plasser pis 8877 2012-03-31 15:11 a.out 
[email protected]$ ./a.out lutsbuf.c 
In Copy_file_to_buf 
SIZE OF THE FILE=1008; fread returned 1. 
Strlen(lutser)=1008 

... 
<contents of file> 
... 
+0

我得到rc等於0. – SlashQB 2012-03-31 12:30:39

+0

在手冊中查看它。這是什麼意思?您正嘗試從N大小的文件中讀取N + 1個字節。請仔細重讀我的回答。 – wildplasser 2012-03-31 12:36:05

+0

我將rc的值設爲1. 'Console Output': 'FILE = 114688; fread返回1.' – SlashQB 2012-03-31 12:48:17

1

檢查的fseek()返回值(和所有其他庫的調用!)

if (fseek(inputFile, 0, SEEK_END)) perror("seek to end"); 
sizeofFile = ftell(inputFile); 
if (sizeofFile == -1) perror("ftell"); 
0

CopyNewFile不關閉它的文件。 WriteSubHeadersToFile可能並不總是(難以遵循流程)。你如何確定文件的「真實」大小?

+0

我用vi編輯器打開文件即'dummyfile'找到'真實'大小。 – SlashQB 2012-03-31 12:29:00

相關問題