2013-02-23 50 views
0

我想將文本文件的內容讀入到二維字符串數組中,但無論我嘗試或搜索了什麼,我都找不到解決方案。該代碼應該加載一個文本文件,通過查找水平製表符並顯示輸出,將其分隔爲多個元素。當我按照原樣運行代碼時,我收到一條從在線搜索中發現的錯誤,這意味着我正試圖操縱我不該做的內存。我並沒有要求編寫代碼,只是朝着正確的方向發展。先謝謝你。使用getline從文件讀取到2d字符串數組

這是我所得到的,當我運行程序:

0x23fd5c 

Process returned -1073741819 (0xC0000005) execution time : 2.344 s 
Press any key to continue. 

編輯::我已經糾正代碼,以便它現在的功能,因爲它應該,但它不是存儲每一行​​的最後一個條目正確的文本文件。我可以以某種方式顯示最後一個條目的數字100,但是當我嘗試傳遞該位置或僅顯示playList [0] [5]時,它表示它等於下一行的第一個條目。任何幫助將是驚人的我發佈了下面的當前代碼。

這裏是我的代碼:

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

void readTextFile(int &Count, string playList[50][5]); 
void userAddition(int &Count, string playList[50][5]); 




int main() 
{ 
string decision; 
string playList[50][5]; 
int Count = 0; 
readTextFile(Count, playList); 

cout << "If you would like to add to the list, Please enter 'Y'. If you would like to exit  
please enter 'N'. ----> "; 
getline(cin, decision); 
if (decision=="y" || decision=="Y") 
    userAddition(Count, playList); 
else 
{ 
    return(0); 
} 

return 0; 
} // End of Main FN. 






void readTextFile(int &Count, string playList[50][5]) 
{ 

string inputfield; 

ifstream infile("c:\\cTunes.txt", ifstream::in); 
    if (infile.is_open()) 
    { 
     // File is read. 
    } // end if 
    else 
    { 
     cout << "Error Opening file" << endl; 
     return; //Program Closes. 
    } // end else 

    cout << setw(30)<<left<< "TITLE"<< setw(10) <<left<<"LENGTH"<< 
     // Outputs a title to   each column that is displayed. 
    setw(40)<< left<<"ARTIST"<< setw(40) << left<<"ALBUM"<< 
    setw(15) << left <<"GENRE" << setw(5) << left << "RATING" << endl; 

getline(infile, inputfield, '\t'); // read until tab 
while(! infile.eof()) // loop until file is no longer valid. 
{ 

     playList[Count][0] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][1] = inputfield; 
     getline(infile, inputfield, '\t');    // read until tab. 

     playList[Count][2] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][3] = inputfield; 
     getline(infile, inputfield, '\t');   // read until tab. 

     playList[Count][4] = inputfield; 
     getline(infile, inputfield);    // read until end of line. 
     playList[Count][5] = inputfield; 

    cout << setw(30)<<left<< playList[Count][0] << setw(10) <<left<<playList[Count][1] <<   
    // Output the line number equal to count. 
    setw(40)<< left<<playList[Count][2] << setw(40) << left<< playList[Count][3] << 
    setw(15) << left << playList[Count][4] << setw(5) << left << playList[Count][5] <<      
    endl; 

    /*cout <<"Title: " << setw(25)<<left<< playList[Count][0]<<endl; 
    cout <<"Length: " << setw(5) <<left<<playList[Count][1] << endl; 
    cout <<"Artist: " << setw(50)<< left<<playList[Count][2] << endl; 
    cout <<"Album: " << setw(40) << left<< playList[Count][3] << endl; 
    cout <<"Genre: " << setw(15) << left << playList[Count][4] << endl; 
    cout <<"Rating: " << setw(5) << left << playList[Count][5] << endl<<endl;*/ 

    Count++;   // Increment counter by 1 
    getline(infile, inputfield, '\t'); // read next line until tab. 

    } // end while 
infile.close(); // close the file being read from. 
cout<<endl<<endl<<playList[0][5]<<endl; 
} // End of readTextFile 

相信看完,直到行結束的時候,但我不知所措我真正的函數getline引起的問題。

+1

'string playList [19] [5];'爲什麼?爲什麼不使用'vector >'? – us2012 2013-02-23 23:31:10

+0

如果您附加了示例輸入+預期輸出,您將得到更適合您的特定情況的答案。 – LihO 2013-02-24 00:05:24

+0

還要注意,當打開文件時出現錯誤:'else {cout <<「Error Opening file」<< endl; }'你應該使用'return'來防止你的函數的其他部分被執行。 – LihO 2013-02-24 00:07:26

回答

0

首先,請注意以下行:

cout << playList << endl; 

正在打印的陣列,而不是內容的地址。您將需要 循環打印內容。

其次,在下面的代碼:

if (infile.is_open()) 
{ 
    // ok, read in the file 
} // end if 
else 
{ 
cout << "Error Opening file" << endl; 
//a return statement is missing 
} 

你需要一個return語句,以避免從一個封閉的流讀取(這可能會導致系統崩潰)

最後,你的函數不返回任何東西,所以它應該被宣佈爲無效 或返回一些值(根據上下文沒有意義)。

希望有所幫助,祝你好運!

+0

謝謝你的幫助。打印地址是否會給我上面發佈的錯誤?當我運行代碼時,windows提示我關閉程序,而不是編譯器,這導致我相信我正在訪問我不應該的東西。我將函數切換爲void,並在錯誤檢查中添加了返回值。 – 2013-02-24 00:34:23

+0

很難說,因爲當我使用minGW編譯器編譯 時,即使在修復之前,您的代碼對我來說工作正常。您是否在使用 visual studio? – 2013-02-24 00:47:21

+0

我已經嘗試過Visual Studio和CodeBlocks :-(我一直試圖讓它工作6小時,我唯一的想法是我的電腦不允許編譯器訪問該文件 – 2013-02-24 00:57:25

0

您正在使用std::string用於存儲字符串,std::ifstream從文件讀取,std::getline閱讀的話...你應該使用,而不是陣列std::vector S:

typedef std::vector<std::string> Line; 
std::vector<Line> playList; 

這將使事情變得更容易了您。 我還建議你改變你從文件中讀取的方式:

while(getline(...)) 
{ 
    ... 
} 

但因爲你不準使用std::vector,你的函數看起來是這樣的:

// reads the content of input file and stores it into playList: 
void readTextFile(std::ifstream& infile, std::string playList[][5]) 
{ 
    std::string line; 
    for (int lineIndex = 0; getline(infile, line); ++lineIndex) 
    { 
     // skip empty lines: 
     if (line.empty()) continue; 

     std::string word; 
     std::istringstream lineStream(line); 
     for (int wordIndex = 0; getline(lineStream, word, '\t'); ++wordIndex) 
     { 
      // skip empty words: 
      if (line.empty()) continue; 

      playList[lineIndex][wordIndex] = word; 
     } 
    } 
} 

可以這樣使用:

std::string playList[19][5]; 

std::ifstream infile("c:\\Test.txt"); 
if (infile.is_open()) 
{ 
    readTextFile(infile, playList); 
    infile.close(); 
} 
else 
    std::cout << "Error Opening file" << std::endl; 

還要注意的是cout << playList << endl;輸出第一個單詞的地址。要打印所有單詞,您必須編寫一個循環。

+0

感謝您的快速響應!我目前在一個非常基礎的C++編程課,我們還沒有包含向量,所以我不認爲這是可以接受的,因爲我不幸地使用它們。 – 2013-02-24 00:01:53

+0

@Davidtftyfy:現在查看我的答案:)請注意,給定的解決方案依賴於文件中每行內不超過5個單詞的事實。 – LihO 2013-02-24 01:08:52

+0

好吧,我會試試看。感謝您投入此時間。我很驚訝這個網站有幫助的人,這是我的第一篇文章,所以我不知道會發生什麼。 – 2013-02-24 01:20:40

0

readTextFile函數不返回任何東西。你需要返回一個字符串。如果你得到錯誤what(): basic_string::_S_construct null not valid那麼這是你的問題。您可以通過使用-Wall編譯器選項來避免類似的問題。

相關問題