2014-11-14 46 views
1

我有一個二進制文件,其中保存我的結構:可用內存,同時讀取二進制文件

struct vec 
{ 
    string author; 
    string name; 
    int pages; 
    string thread; 
    vec *next; 
}; 

寫入文件功能:

void logic::WriteInfoToFile() 
{ 
    if (first != NULL) 
    { 
     pFile = fopen(way.c_str(), "wb"); 
     if (pFile != NULL) 
     { 
      fseek(pFile, 0, SEEK_SET); 
      temp = first; 
      while (temp != NULL) 
       { 
        WriteString(temp->author,pFile); 
        WriteString(temp->name,pFile); 
        fwrite(&temp->pages,sizeof(int), 1, pFile); 
        WriteString(temp->thread,pFile); 
        temp = temp->next; 
       }  
     } 
     fclose(pFile); 
    } 
} 

寫srtig功能:

void logic::WriteString(string s, FILE *pFile) 
{ 
    if (pFile != NULL) 
    { 
     char *str = new char[s.length() + 1]; 
     strcpy(str, s.c_str()); 
     int size = strlen(str); 
     fwrite(&size, sizeof(int), 1, pFile); 
     fwrite(str, size, 1, pFile); 
     delete [] str; 
    } 
} 

讀取文件:

void logic::ReadInfoFromFile() 
{ 
    pFile = fopen(way.c_str(), "rb"); 
    if (pFile != NULL) 
    {  
     fseek(pFile, 0, SEEK_END); 
     if (ftell(pFile) != 0) 
     { 
      fseek(pFile, 0, SEEK_SET); 
      int check; 
      while (check != EOF) 
      //while (!feof(pFile)) 
      { 
       temp = new vec; 
       temp->author = ReadString(pFile); 
       temp->name = ReadString(pFile); 
       fread(&temp->pages, sizeof(int), 1, pFile); 
       temp->thread = ReadString(pFile); 
       temp->next = NULL; 
       if (first == NULL) 
       { 
        first = temp; 
        first->next = NULL; 
       } 
       else 
       { 
        temp->next = first; 
        first = temp; 
       } 
       recordsCounter++; 

       check = fgetc(pFile); 
       fseek(pFile, -1, SEEK_CUR); 
      } 
     } 
    } 
    fclose(pFile); 
} 

讀字符串:

string logic::ReadString(FILE *pFile) 
{ 
    string s; 
    if (pFile != NULL) 
    { 
     int size = 0; 
     fread(&size, sizeof(int), 1, pFile); 
     char *str = new char[size]; 
     fread(str, size, 1, pFile); 
     str[size] = '\0'; 
     s = str; 
     //delete [] str; //WHY?????????!!!!! 
     return s; 
    } 
    else 
     return s = "error"; 
} 

麻煩的是在讀字符串函數,我在那裏的可用內存。 「刪除[] str」我在這一行遇到了程序崩潰。

但如果我不豁免記憶工作良好。

請幫幫我!

+0

爲什麼你要做所有這些動態分配來編寫std :: string?只需使用'c_str()'和'string :: size()'。所有關於字符串的信息都在這兩個函數中。 – PaulMcKenzie 2014-11-14 12:44:17

+0

您的代碼有幾個問題,主要是因爲您將C函數與C++代碼混合在一起。一個(次要的)問題是['std :: strlen'](http://en.cppreference.com/w/cpp/string/byte/strlen)不會返回int,而是std :: size_t'。 – 2014-11-14 12:44:44

+0

如果您學會使用[C++輸入/輸出庫](http://en.cppreference.com/w/cpp/io),並且使用['std ::],那麼許多問題會自動消失。字符串'](http://en.cppreference.com/w/cpp/string/basic_string)而不是'char *'。 – 2014-11-14 12:45:21

回答

3

你關閉由一個分配size字符但重寫大小+ 1(與終端「\ 0」)。內存管理器不喜歡這樣。

char *str = new char[size]; 
fread(str, size, 1, pFile); 
str[size] = '\0' 
+0

謝謝,現在它的工作! – mrglut 2014-11-14 12:50:02

+0

如果'new []'拋出異常會發生什麼? – PaulMcKenzie 2014-11-14 12:57:29

+0

@PaulMcKenzie http://stackoverflow.com/questions/9456728/how-to-deal-with-bad-alloc-in-c – Oncaphillis 2014-11-14 13:02:20