从文件中读取时,如何跳过数组的第一行
How do I skip the first line of an array when reading from a file?
我试图从文本文件中读取值,然后将它们粘贴到数组中。我使用的文本文件是这样的;
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];
}
您可以跳过第一行,并执行以下操作:
inData.ignore(numeric_limits<streamsize>::max(), inData.widen('n'));
std::istream::忽略引用:http://www.cplusplus.com/reference/istream/istream/ignore/
PS。第一个参数是要忽略的最大字符数。numeric_limits:max((表示不管字符串有多长,都要忽略直到字符串结束
这里有一个答案:如何使用fstream从第二行读取文本文件?
在执行getData调用之前,只需阅读第一行即可。
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;
溪流的行为与其同名。你不能像跳过数组一样跳过流中的内容,你所能做的就是在它经过时忽略它。
您可以使用seekg((函数来移动FPM(即文件位置标记(所需的任意空间。这可能是最简单的方法
从你的代码来看,它看起来像
inData.seekg(16, std::ios::beg) //Seeks past 16 character in the file
相关文章:
- C++找出覆盖同一行的数组
- 如何让字符数组在一行而不是多行中打印出来?
- 在 c++ 中将uint8_t数组转换为字符串以在一行中打印
- 在一行中初始化和引入数组向量的正确方法?
- 将上一行添加到 char* 数组中的 char* 常量
- 将 ifstream 文件和每一行读取到数组中
- 在 C++ 中搜索 2D 数组以查看一行/列是否与另一行/列相同
- 与 C++ 中一行中的用户输入数组元素相关的问题
- 将地址返回到 2D 数组中的一行值的函数
- C ++试图找到数组中每一行的最大值
- 如何在一行中从不同的变量(数据类型)创建一个字符数组?
- C++ ->尝试逐字阅读一行文本。如何使指针等效于用于存储输入的当前二维数组
- C++ 哪些列在 2 dim 数组中的每一行都有 1
- 按一行对2D数组进行排序
- 无法弄清楚为什么只从文件中读取一行矩阵以及为什么我无法将 2D 数组传递给函数
- 打印一行 2D 字符数组 (C++)
- 如何将.txt文件中的多维数组放入代码中,并一次打印一行
- C++ - 将数字表从文件读取到 2D 数组中(仅存储最后一行)
- 在同一行中读取混合输入(字符串/浮点数),并将其存储在数组中
- 如何在每一行数组C++中进行颜色更改