0

我正在尝试将文本文件的内容读入二维字符串数组,但无论我尝试或搜索了什么,我都找不到解决方案。该代码应该加载一个文本文件,通过查找水平选项卡将其分成元素并显示输出。当我按原样运行代码时,我收到一个从在线搜索中发现的错误,这意味着我正在尝试操作我不应该操作的内存。我不是要求编写代码,只是朝着正确的方向推动。先感谢您。

这是我运行程序时得到的:

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 在阅读到行尾时会导致问题,但我真的很茫然。

4

3 回答 3

0

std::string用于存储字符串、std::ifstream从文件读取、std::getline读取单词……您应该真正使用std::vectors 而不是数组:

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;输出第一个字的地址。要打印所有单词,您必须编写一个循环。

于 2013-02-23T23:36:17.240 回答
0

readTextFile函数不返回任何内容。您需要返回一个字符串。如果您收到错误,what(): basic_string::_S_construct null not valid那么这就是您的问题。您可以通过使用-Wall编译器选项来避免类似的问题。

于 2013-02-23T23:46:11.803 回答
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 语句以避免从关闭的流中读取(这可能导致崩溃)

最后,你的函数不返回任何东西,所以它应该被声明为 void 或者返回一些值(根据上下文没有意义)。

希望有帮助,祝你好运!

于 2013-02-24T00:27:38.930 回答