2009-04-19 116 views
0

任何人都知道如何將指針存儲在多維數組中?我認爲這可能是我在主時遇到問題:指針問題

// main.cpp 
#ifdef _DEBUG 
#define _CRTDBG_MAP_ALLOC 
#include <iostream> 
#include <fstream> 
#include <string> 
#endif 
#include "Word.h" 
using namespace std; 

const int WORD_SZ = 100; 
Word** g_wordArray; 
int g_arrSz; 

static char filePath[ FILE_PATH_SZ ] = {}; 
void FreeWordArray(); 

int main(const int argc, const char **argv) 
{ 
    int 
     wrdCount = 0; 
    char 
     usrMenuOption  = 0, 
     getFirstLetter = 0, 
     tmpArray[WORD_SZ] = {}, 
     *getWord = new char; 
    string 
     str, 
     str2; 
    ifstream 
     inFile, 
     inFile2; 
    do 
    { 
     cout << "Please make a selection: \n\ 
a) Read a text file\n\ 
b) Remove words starting with letter\n\ 
c) Print words to console\n\ 
d) Quit\n"; 
     cin >> usrMenuOption; 
     switch(usrMenuOption) 
     { 
     case'A': 
     case'a': 
      cout << "Enter a file name: "; 
      cin.sync(); 
      cin >> filePath; 
      inFile.open(filePath); 
      if (!inFile) return -1; 
      inFile >> tmpArray; // prime the eof flag. 
      while (!inFile.eof()) 
      { 
       inFile >> tmpArray; 
       wrdCount++; 
       g_wordArray = new Word *[wrdCount]; 

      } 
     inFile.close(); 
     inFile2.open(filePath); 
     while(!inFile2.eof() ) 
     { 
      inFile2 >> tmpArray; 
      // supplies the member functions with information from the file 
      g_wordArray[wrdCount] = new Word(tmpArray); 
      g_wordArray[wrdCount]->GetFirstLetterLower(); 
      g_wordArray[wrdCount]->GetWord(); 
     } 
     cout << wrdCount << " Words read from the file " << endl; 
     inFile2.close(); 
     break; 
     case'B': 
     case'b': 
     // information not found returning null 
       g_wordArray[wrdCount]->GetFirstLetterLower(); 
     break; 
     case'C': 
     case'c': 
       g_wordArray[wrdCount]->GetWord(); 
     break; 
     case'D': 
     case'd': 
     cout << "Quit Requested. " << endl; 
     break; 
     default: 
     cout << '"' << usrMenuOption << '"' << " Not Defined! " << endl; 
     } 

    } while (usrMenuOption != 'D' && usrMenuOption != 'd'); 


#ifdef _DEBUG 
    _CrtDumpMemoryLeaks(); 
#endif 
    cin.ignore(); 
    return 0; 
} 

void FreeWordArray() 
{ 
    delete[ ] g_wordArray; 
    return; 
} 


// Word.cpp 
#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy 
#define ARRY_SZ 100 
#include <iostream> 
#include <fstream> 
#include "Word.h" 
#include <vector> 


using namespace std; 

// No paramaters. 
// Is this what im missing? 
// I just threw it in because of an error. 
Word::Word() 
{ 
} 

Word::Word(const char* word) 
{ 
    ptr_ = new char[ strlen(word) + 1 ]; 
    strcpy(ptr_, word ); 
    len_ = strlen(ptr_); 
} 

Word::~Word() 
{ 
    delete[ ] ptr_; 
    ptr_ = NULL; 
    len_ = NULL; 
} 

char Word::GetFirstLetterLower() 
{ 
    char myChar = tolower(ptr_[0]); 
    return myChar; 

} 

char* Word::GetWord() 
{ 
    Word *objectOne = new Word; 
    objectOne->ptr_ = ptr_; 
    strcpy(objectOne->ptr_, ptr_); 
    return objectOne->ptr_; 
} 

我的目標是讓從文件中讀取所有單詞我g_wordArray[wrdCount]->SomeFunction()不依賴於文件讀取循環。

什麼,我一直在努力做的事情:

  • 在實現文件中,下getFirstLetterLower:每次char *_ptr私有成員添加到一個新的變量。如someCharVar[0] = firstWordsomeCharVar[1] = secondWord ...

  • 將文件的內容讀取到單個變量中。在每種情況下,我都需要通過這個變量來循環。

我喜歡這個想法,但還沒有想出如何做到這一點。有什麼建議麼?

+0

我建議你試着解釋你到底有什麼問題 - 從你的問題 – 2009-04-19 05:41:51

+0

...以及你正在嘗試做什麼(不是代碼,但是你正在嘗試解決的問題)中並不清楚。對於你所知道的,可能有另一種方法! – dirkgently 2009-04-19 06:39:41

回答

-2

你的問題到底是什麼?你在哪裏得到一個錯誤?

您是否考慮過縮短main()並將您的工作放在其他功能中?這段代碼很難閱讀。 另外,do ... while循環確實不好用,請考慮切換到while循環。你需要某種布爾值,但爲了可讀性,這是值得的。

1

請發佈可重現您的問題的最小代碼。現在看起來很朦朧。我想這個代碼有很多問題

 inFile >> tmpArray;  // prime the eof flag. 
     while (!inFile.eof()) 
     {  
       inFile >> tmpArray; 
       wrdCount++; 
       g_wordArray = new Word *[wrdCount]; 

     } 

你在這裏泄漏內存不好。先前分配的'g_wordArray'會發生什麼?

此外,當您分配'n'個元素時,無法訪問'第n'個索引。內存佔用範圍從0 - (n-1)。

g_wordArray[wrdCount] 

請重新檢查代碼,儘量調試,然後張貼最少的代碼。

1

IIUC,您正在嘗試讀取文件並可選擇刪除以特定字母開頭的一些單詞。這裏有一個簡單的例子,甚至沒有基本的錯誤處理,你會如何實現使用STL相同,一些模板:

#include <iterator> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <fstream> 
#include <iostream> 

struct starts_with { 
    char mC; 
    starts_with(char c) : mC(c) {} 
    bool operator()(std::string const& s) { return s[ 0 ] == mC; } 
}; 

// uses commandline parameters for input arguments 
// usage: 
// ./test.exe "path/to/file" letter 
// 
// (assumes the file to be read is the first such parameter 
// the character to be checked for is the second parameter) 
int main(int argc, char *argv[ ]) { 
    using namespace std; 
    vector<string> v; 
    ifstream fStm(argv[ 1 ]); 
    istream_iterator<string> b(fStm), e; 

    remove_copy_if(b, e, back_inserter(v), starts_with(argv[ 2 ][ 0 ])); 

    copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n")); 

    return 0; 
} 
1

這是用於讀取文件中的常見反模式。
請注意,wrdCount始終是一個關閉,因爲您在循環中檢測文件結尾(EOF)的同時將該單詞讀入tmpArray。

inFile >> tmpArray;  // prime the eof flag. 
while (!inFile.eof()) 
{  
    inFile >> tmpArray; 
    wrdCount++; 
    g_wordArray = new Word *[wrdCount]; 
} 

用這個代替:

/* 
* The result of the >> operator is a reference to a stream. 
* 
* When a stream is used in a boolean context (like a while loop) 
* it has a cast method that automatically returns an object 
* of a type that can be used as a boolean. 
* 
* The value of the boolean will be true if the file is still valid 
* and false if something went wrong (like reading past the end of file). 
* 
* So the loop will NOT be entered when you read past the EOF but will 
* be entered for every valid word that is read from the file. 
*/ 
while (inFile >> tmpArray) 
{  
    wrdCount++; 
    g_wordArray = new Word *[wrdCount]; 
} 
1

你的索引到你的陣列永遠是錯的:

g_wordArray[wrdCount]->GetFirstLetterLower(); 

這裏wrdCount'是 'g_wordArray' 元素的數量因此產生的'g_wordArray [wrdCount]'正在訪問超出數組末尾的元素。

記住,C/C++數組索引從0並且因此具有有效元素0 - >(wrdCount -1)

0

您正在泄漏內存所有的地方。
你應該嘗試使用STL標準容器來處理這種類型的事情。

Word** g_wordArray; 

這可能是更好地表示:

std::vector<Word> g_wordArray; 

然後,而不是對數組這樣分配空間:

g_wordArray = new Word *[wrdCount]; 
----- 
g_wordArray.reserve(wrdCount); 

而且lastely添加新元素的變化在於這一點:

g_wordArray[wrdCount] = new Word(tmpArray); 
g_wordArray[wrdCount]->GetFirstLetterLower(); 
g_wordArray[wrdCount]->GetWord(); 

------ 

g_wordArray.push_back(Word(tmpArray)); 
g_wordArray[wrdCount].GetFirstLetterLower(); // Note we use . not -> 
g_wordArray[wrdCount].GetWord(); 

現在你的代碼不再使用任何討厭的新操作符,因此你不會泄漏任何內存。