텍스트 파일의 내용을 2D 문자열 배열로 읽으려고 시도했으나 시도했거나 검색 한 내용과 상관없이 해결책을 찾을 수 없습니다. 이 코드는 가로 탭을 찾아 출력을 표시하여 텍스트 파일을 요소로 분리하여로드해야합니다. 있는 그대로 코드를 실행하면 온라인 검색에서 발견 된 오류가 발생합니다. 따라서 내가해서는 안되는 메모리를 조작하려고합니다. 필자는 코드 작성을 요구하지 않고 올바른 방향으로 밀어 넣을 것을 요구합니다. 미리 감사드립니다. getline을 사용하여 파일에서 2 차원 문자열 배열로 읽기
이
내가 프로그램을 실행할 때 내가 무엇을 얻을 수 있습니다 :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]; '왜?! 왜 '벡터 <벡터>'을 사용하지 않겠습니까? –
us2012
예제 입력 + 예상 출력을 첨부하면 특정 상황에 더 적합한 대답을 얻을 수 있습니다. – LihO
또한 파일을 여는 동안 오류가 발생했을 때 :'else {cout << "Error Error opening file"<< endl; }'나머지는 당신의 함수가 실행되는 것을 막기 위해'return'을 사용해야합니다. – LihO