Josh
Josh

Reputation: 71

How do I skip the first line of an array when reading from a file?

I'm trying to read values from a text file and then stick them into an array. The text file I'm using is like this;

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

I'm trying to read them into an array, and then output the array. I have to do other stuff, (alter the values from user input), but I know how to do that. My problem is this; When I run it, it reads Big and Stone, and puts them into the values. This isn't what I want; It should be something like Red_Tailed_Hawk and 1. I have my code below; I have no idea what to do to get it to work right. Please excuse some of the variables; I cannibalized this script from another project I did with int arrays instead of string arrays.

#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];
}

Upvotes: 1

Views: 3882

Answers (4)

Arthur Golubev 1985
Arthur Golubev 1985

Reputation: 677

You can skip the first line with the following:

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

std::istream::ignore reference: http://www.cplusplus.com/reference/istream/istream/ignore/

PS. The first parameter is a maximum number of chars to ignore. numeric_limits::max() means that you want to ignore until the string ends, no matter how long the string is.

Upvotes: 2

Jainathan Leung
Jainathan Leung

Reputation: 1157

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;

Streams behave like their namesake. You can't skip what comes in the stream like jumping through an array, all you can do is ignore stuff as it floats by.

Upvotes: 0

MrPickle5
MrPickle5

Reputation: 522

You can use the seekg() function which moves the FPM (i.e. File Position Marker) any amount of space you want. That is probably the easiest way to do it.

From you code it would look something like

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

Upvotes: -1

dan
dan

Reputation: 1002

There's an answer here: How do I read a text file from the second line using fstream?

Just read the first line before doing the getData call.

Upvotes: 1

Related Questions