我是C ++的新手,所以请给我一个改变来学习!
假设我有一个文本文件RoomDB.txt,它以下列格式存储数据:
005:Room 1:Level 1:3-Jul-14:0900:3:Mathematics 101:Julia Lee
006:Room 2:Level 2:2-Jun-14:0800:2:English 101:Jared Loo
007:Room 3:Level 3:15-Apr-14:1800:3:Chinese 101:David Tay
008:Room 4:Level 4:15-Apr-14:1200:3:Science 101:Michelle Choo
如何根据第四个字段(日期)选择性地显示特定行(或更多)数据,使其看起来像这样 - (如果我只想显示日期为15的行-Apr-14)
Booking ID Room No. Level No. Time Duration Subject Lecturer
----------------------------------------------------------------------------------------
007 Room 3 Level 3 1800 3 Chinese 101 David Tay
008 Room 4 Level 4 1200 3 Science 101 Michelle Choo
PS:请注意,日期从所需输出中排除,因为它将用作选择要显示的数据的标准。我目前的代码如下:
int main()
{
cout << "\nBooking ID Room No. Level No. Time Duration Subject Lecturer" << endl;
cout << "-------------------------------------------------------------------------------" << endl;
string array[50]; // creates array to hold names
short loop=0; //short for loop for input
string line; //this will contain the data read from the file
ifstream myfile ("RoomDB.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
array[loop] = line;
cout << "At element " << loop << ", value is :" << array[loop] << endl;
loop++;
}
myfile.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output
}
答案 0 :(得分:0)
这是一个类似CSV的文件,带有分隔符:
,您可以:
- 使用std :: getline
逐行读取文件
- 使用Boost String Algorithm按:
分割线
- 在结构或集合中加载以进行处理
- 输出必要信息