2013-09-29 42 views
0
if (fwarrior != NULL) { 
/* Go to the end of the file. */ 

if (fseek(fwarrior, 0, SEEK_END) == 0) { 
/* Get the size of the file. */ 
long bufsize = ftell(fwarrior); 
if (bufsize == -1) 
{ 
    printf("Error in reading the size of file"); 
} 

/* Allocate our buffer to that size. */ 
buffer = malloc(sizeof(char) * (bufsize + 1)); 

/* Go back to the start of the file. */ 
if (fseek(fwarrior, 0, SEEK_SET) == 0) 
{ 
    printf("Error = File is considered NULL"); 
} 

/* Read the entire file into memory. */ 
size_t newLen = fread(buffer, sizeof(char), bufsize, fwarrior); 

if (newLen == 0) 
{ 
     fputs("Error reading file", stderr); 
} 

else 
{ 
     buffer[newLen] = '\0'; /* Just to be safe. */ 
} 

} 
} 

此代碼是通過VS2012在Windows上運行,這似乎是"Error = File is considered NULL"錯誤,把文本文件的大小爲緩衝區

我想某個文件的內容分配到緩衝區和使用之後創建一個單獨鏈接列表以將文件的內容分配到它們各自的變量中。

我剛剛從一個網站上面得到了上面的代碼,這個代碼的製造者說上面的代碼基本上讀取了文件的全部內容,然後將它們分配到緩衝區中,以便我們稍後可以使用sscanf

樣品:

#include <stdio.h> 
#include <stdlib.h> 
typedef struct hero_data{ 
    char name[254]; 
    char title[254]; 
    int encoding; 
    int startstr; 
    double incstr; 
    int startdex; 
    double incdex; 
    int startintel; 
    double incintel; 
    int basemindmg,basemaxdmg; 
    double bat; 
    double basearmor; 
    struct hero_data *next; 
    struct hero_data *type; 
    struct hero_data *Class; 
}hero; 
int main() 
{ 
hero *curr[] = {0}; 
hero *head; 
hero *tail; 
int totalwarrior = 0; 
FILE *fwarrior; 
char filename[256]; 
char *buffer = NULL; 
size_t newLen; 
printf("Name of Roster File for Warriors?(without .extension): \n"); 
scanf("%s",&filename); 
strcat(filename,".txt"); 
fwarrior = fopen(filename,"r"); 

if (fwarrior != NULL) { 
    /* Go to the end of the file. */ 
    if (fseek(fwarrior, 0, SEEK_END) == 0) { 
     /* Get the size of the file. */ 
     long bufsize = ftell(fwarrior); 
     if (bufsize == -1) 
     { 
      printf("Error in reading the size of file"); 

     } 

    /* Allocate our buffer to that size. */ 
    buffer = malloc(sizeof(char) * (bufsize + 1)); 

    /* Go back to the start of the file. */ 
    if (fseek(fwarrior, 0, SEEK_SET) == 0) //So is this the right way to use fseek? 
    { 

    /* Read the entire file into memory. */ 
    size_t newLen = fread(buffer, sizeof(char), bufsize, fwarrior); 

    if (newLen == 0) 
    { 
     fputs("Error reading file", stderr); 
    } 
    else 
    { 
     buffer[newLen] = '\0'; /* Just to be safe. */ 
    } 
} 
} 

while(fgets(buffer, sizeof buffer, fwarrior) != NULL) 
{ 
if(13 == sscanf(buffer,"%s[^,] , %s[^,] , %d , %d , %lf , %d , %lf , %d , %lf , %d , %d , %lf , %lf", &curr[totalwarrior]->name,&curr[totalwarrior]->title,&curr[totalwarrior]->encoding,&curr[totalwarrior]->startstr,&curr[totalwarrior]->incstr,&curr[totalwarrior]->startdex,&curr[totalwarrior]->incdex,&curr[totalwarrior]->startintel,&curr[totalwarrior]->incintel,&curr[totalwarrior]->basemindmg,&curr[totalwarrior]->basemaxdmg,&curr[totalwarrior]->bat,&curr[totalwarrior]->basearmor)) 
{ 
    if (head == NULL) 
    { 
     head = curr[totalwarrior];  
    } 
    else 
    { 
     tail->next=curr[totalwarrior]; 
    } 
    tail = curr[totalwarrior]; 

    if (head!=NULL) 
    { 
     curr[totalwarrior] = head; 
     do { 
     printf("%s\n%s\n%d\n%d\n%.1lf\n%d\n%.1lf\n%d\n%.1lf\n%d\n%d\n%.1lf\n%.1lf\n\n",curr[totalwarrior]->name,curr[totalwarrior]->title,curr[totalwarrior]->encoding,curr[totalwarrior]->startstr,curr[totalwarrior]->incstr,curr[totalwarrior]->startdex,curr[totalwarrior]->incdex,curr[totalwarrior]->startintel,curr[totalwarrior]->incintel,curr[totalwarrior]->basemindmg,curr[totalwarrior]->basemaxdmg,curr[totalwarrior]->bat,curr[totalwarrior]->basearmor); 
     curr[totalwarrior] = curr[totalwarrior]->next = curr[totalwarrior+1]; 
     curr[totalwarrior] = NULL; 
     printf("Before Total %d\n",totalwarrior); 
     totalwarrior++; 
     printf("Now Total %d\n",totalwarrior); 
     }while(curr[totalwarrior-1] != NULL); 
    } free(curr[totalwarrior]); 
} 
} 
} 
} 
//I created a typedef struct named curr that is in the form of array. It is legit. 
//I do not know the limit of the file so that's why I am trying to use the function above to get the size of the file in order to use `sscanf`. 

//I made the code `13 == sscanf(...)` because within the file, there will be multiple "sets" of data that are in the set of 13 variables. Everytime when the program reads the 13th variable, I will add counter to `[totalwarrior]` of the typedef struct named `curr`. 

下面是該程序將讀取樣本文件:

Relay Chestdrop 
Snow Damsel 
230 
16 
1.7 
16 
1.6 
21 
2.9 
38 
44 
1.7 
1.24 
Linnea Reversa 
Vanquisher 
214 
18 
1.5 
16 
1.5 
27 
3.2 
40 
58 
1.7 
1.24 
Kyle 
Hoarder 
255 
19 
1.7 
20 
1.9 
22 
2.5 
35 
41 
1.7 
1.8 
Leo 
Wiccan Monster 
236 
16 
1.7 
15 
1.5 
22 
3 
42 
48 
1.7 
1.1 
Changer 
Changer 
191 
19 
2 
24 
3 
17 
1.5 
37 
46 
1.6 
1.36 
Consortio Piercoptic 
Elvish Sharpshooter 
177 
16 
1.7 
21 
2.9 
15 
2.6 
36 
42 
1.7 
1.94 
Tinklez 
Osteoarcher 
149 
15 
1.6 
22 
3 
16 
1.55 
37 
43 
1.7 
2.08 
Weeeeheeee-urrr 
Silhouette Devil 
180 
15 
2 
20 
2.9 
18 
2 
35 
41 
1.7 
1.8 
Deckard Haemofoot 
Bovine Captain 
71 
24 
2.3 
14 
1.5 
23 
1.6 
47 
57 
1.7 
2.96 
Shelgar 
Blessed Hero 
92 
21 
2.4 
20 
2.4 
18 
1.5 
42 
51 
1.6 
1.8 
Tomahawk 
Genghis Tycoon 
120 
25 
2.5 
20 
2.2 
18 
1.6 
49 
53 
1.7 
1.8 
Beelzebub 
Ruinbearer 
108 
26 
3.2 
11 
0.9 
13 
2.1 
53 
69 
1.7 
0.54 
+0

後最短的例子表明,我們實際上可以編譯的問題。 –

+0

發佈與你的問題相關的部分 –

+0

這個:'buffer [++ newLen] ='\ 0';'應該是這個:'buffer [newLen] ='\ 0';'順便說一句。如果讀取10個字節,則需要將'buffer [10]'設置爲'\ 0',而不是'buffer [11]'。如果你真的在這裏讀取'bufsize'字節,你會寫入緩衝區的末尾。它也不是「僅僅爲了安全」 - 這是必要的(如果你至少處理字符串),因爲'fread()'不會爲你添加終止NUL。 –

回答

1

fseek運行正常,但你曲解的結果。

返回值

如果成功,函數返回0。 否則,它返回非零值。 如果發生讀取或寫入錯誤,則設置錯誤指示符(ferror)。

(從CPP Reference