使用getline从文件读取到2d字符串数组

时间:2013-02-23 23:29:40

标签: c++ arrays string file-io text-files

我正在尝试将文本文件的内容读入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在读到行结束时会引起问题,但我真的不知所措。

3 个答案:

答案 0 :(得分:0)

您正在使用std::string来存储字符串,std::ifstream用于从文件中读取,std::getline用于读取字词...您应该使用std::vector代替阵列:

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

答案 1 :(得分:0)

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

答案 2 :(得分: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 或者返回一些值(根据上下文没有任何意义)。

希望有所帮助,祝你好运!