2013-04-06 2 views
1

텍스트 파일에서 값을 읽은 다음 배열에 붙여 넣으려고합니다. 내가 사용하고있는 텍스트 파일은 다음과 같습니다.파일을 읽을 때 배열의 첫 번째 줄을 건너 뛰려면 어떻게합니까?

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 

배열로 읽어 들이고 배열을 출력하려고합니다. 나는 다른 것들을해야만한다. (사용자 입력으로부터 값을 바꾼다.) 그러나 나는 그것을 어떻게하는지 안다. 내 문제는 이것입니다. 나는 그것을 실행할 때 BigStone을 읽고 값에 넣습니다. 이것은 내가 원하는 것이 아닙니다. 그것은 Red_Tailed_Hawk1과 같아야합니다. 아래 코드가 있습니다. 나는 그것이 올바르게 작동하도록하기 위해 무엇을해야하는지 잘 모른다. 변수 중 일부는 변명하십시오. 문자열 배열 대신 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]; 
} 

답변

1
string line;  
getline(infile, line) 
for (row = 0; row < row_count; row++) 
     for (col =0; col < column_count +1 ; col++) 
      getline(infile, line); 
      chart[row][col] = line; 

스트림은 자신의 이름을 딴 사람처럼 행동. 스트림을 통해 배열을 뛰어 넘는 것과 같이 스트림에서 오는 것을 건너 뛸 수 없으며, 수레가 플로트 할 때 무시할 수 있습니다.

+0

완벽하게 작동했습니다. 고맙습니다! – Josh

0

FPM (File Position Marker)을 원하는만큼의 공간으로 이동시키는 seekg() 함수를 사용할 수 있습니다. 아마도 그렇게하는 것이 가장 쉬운 방법 일 것입니다.

당신이 당신이 다음에 첫 번째 줄 건너 뛸 수 있습니다

inData.seekg(16, std::ios::beg) //Seeks past 16 character in the file 
2

처럼 보일 것입니다 코드에서 :

inData.ignore(numeric_limits<streamsize>::max(), inData.widen('\n')); 

표준 : istream로를 :: 무시 참조 : http://www.cplusplus.com/reference/istream/istream/ignore/

추신. 첫 번째 매개 변수는 무시할 최대 문자 수입니다. numeric_limits :: max()는 문자열의 길이와 상관없이 문자열이 끝날 때까지 무시하려는 것을 의미합니다.

관련 문제