我想将文本文件的内容读入到二维字符串数组中,但无论我尝试或搜索了什么,我都找不到解决方案。该代码应该加载一个文本文件,通过查找水平制表符并显示输出,将其分隔为多个元素。当我按照原样运行代码时,我收到一条从在线搜索中发现的错误,这意味着我正试图操纵我不该做的内存。我并没有要求编写代码,只是朝着正确的方向发展。先谢谢你。使用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引起的问题。
'string playList [19] [5];'为什么?为什么不使用'vector>'? –
us2012
2013-02-23 23:31:10
如果您附加了示例输入+预期输出,您将得到更适合您的特定情况的答案。 – LihO 2013-02-24 00:05:24
还要注意,当打开文件时出现错误:'else {cout <<“Error Opening file”<< endl; }'你应该使用'return'来防止你的函数的其他部分被执行。 – LihO 2013-02-24 00:07:26