텍스트 파일에서 값을 읽은 다음 배열에 붙여 넣으려고합니다. 내가 사용하고있는 텍스트 파일은 다음과 같습니다.파일을 읽을 때 배열의 첫 번째 줄을 건너 뛰려면 어떻게합니까?
Big Stone Gap,VA
Red-tailed_Hawk 1
Mourning_Dove 66
Red-bellied_Woodpecker 5
Yellow-bellied_Sapsucker 1
Downy_Woodpecker 4
Pileated_Woodpecker 2
Blue_Jay 8
American_Crow 89
Carolina_Chickadee 8
Black-capped_Chickadee 6
Tufted_Titmouse 12
Red-breasted_Nuthatch 2
White-breasted_Nuthatch 9
Carolina_Wren 3
American_Robin 1
Northern_Mockingbird 1
European_Starling 5
Eastern_Towhee 3
Field_Sparrow 13
Fox_Sparrow 1
Song_Sparrow 8
White-throated_Sparrow 11
Dark-eyed_Junco 9
Northern_Cardinal 30
Purple_Finch 7
House_Finch 6
American_Goldfinch 29
배열로 읽어 들이고 배열을 출력하려고합니다. 나는 다른 것들을해야만한다. (사용자 입력으로부터 값을 바꾼다.) 그러나 나는 그것을 어떻게하는지 안다. 내 문제는 이것입니다. 나는 그것을 실행할 때 Big
과 Stone
을 읽고 값에 넣습니다. 이것은 내가 원하는 것이 아닙니다. 그것은 Red_Tailed_Hawk
과 1
과 같아야합니다. 아래 코드가 있습니다. 나는 그것이 올바르게 작동하도록하기 위해 무엇을해야하는지 잘 모른다. 변수 중 일부는 변명하십시오. 문자열 배열 대신 int 배열을 사용하여 수행 한 다른 프로젝트에서이 스크립트를 제거했습니다.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int NUMBER_OF_STUDENTS = 28;
const int NUMBER_OF_SCORES = 28;
void getData (ifstream& infile, string matrix[][NUMBER_OF_SCORES + 1 ],
int NUMBER_OF_STUDENTS) ;
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS);
int main()
{
string birdarray [NUMBER_OF_STUDENTS][NUMBER_OF_SCORES +1 ] ;
// column 0 will hold the student ID.
// row n has the ID and birdarray for student n.
ifstream inData; //input file stream variable
inData.open("one.txt");
if (!inData)
{
cout << "invalid file name \n";
return 1;
}
// input the birdarray into two-D array birdarray
getData (inData , birdarray, NUMBER_OF_STUDENTS);
printMatrix(birdarray, NUMBER_OF_STUDENTS);
// return the row number of a searchItem in a particular column.
// if not found, return -1
}
void getData (ifstream& infile,string chart[][NUMBER_OF_SCORES + 1 ],
int student_count)
{
int row, col;
for (row = 0; row < student_count; row++)
for (col =0; col < NUMBER_OF_SCORES +1 ; col++)
infile >> chart [row] [col] ;
}
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
int row, col;
//this code below is to loop it so it displays all the values
// for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++)
// {
// cout << right << matrix[row][col];
// cout << endl;
//}
//this is just some test code to see if it can output certain values right
cout << matrix[0][2];
cout << matrix[0][3];
cout << matrix[0][4];
cout << matrix[0][5];
}
완벽하게 작동했습니다. 고맙습니다! – Josh