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

我正在尝试将它们读入一个数组,然后输出该数组。我必须做其他事情,(改变用户输入的值),但我知道该怎么做。我的问题是这个;当我运行它时,它会读取Bigand Stone,并将它们放入值中。这不是我想要的;它应该类似于Red_Tailed_Hawkand 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];
}
4

4 回答 4

2

您可以使用以下内容跳过第一行:

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

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

PS。第一个参数是要忽略的最大字符数。numeric_limits::max() 表示您要忽略直到字符串结束,无论字符串有多长。

于 2014-08-02T01:26:51.367 回答
1

这里有一个答案: How do I read a text file from the second line using fstream?

在进行 getData 调用之前,只需阅读第一行。

于 2013-04-06T00:16:42.553 回答
0
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;

流的行为与其同名。您不能像跳过数组一样跳过流中的内容,您所能做的就是忽略漂浮的内容。

于 2013-04-06T00:14:40.713 回答
-1

您可以使用 seekg() 函数将 FPM(即文件位置标记)移动到您想要的任何空间量。这可能是最简单的方法。

从你的代码来看,它看起来像

inData.seekg(16, std::ios::beg) //Seeks past 16 character in the file
于 2013-04-06T00:47:39.593 回答