我被指派为订房系统编写一个c++程序。我知道如何逐行读取文本文件并保存。但我的问题是如何逐字阅读文本文件。
这是我的文本文件:
1-Reserved-2018-12-23-Lecture Room-13
2-Reserved-2018-11-34-Tutorial Room-15
3-Not Reserved-0-0-0-Design Studio-18
4-Reserved-2018-11-16-Lecture Room-14
5-Not Reserved-0-0-0-Exam Hall-18
我想读取文本文件并将单词保存到它的节点中。 (如 ID
、data
、typeofRoom
等)在 C++ 中有什么方法可以这样做吗?
这是我的课:
class room {
public:
int length;
int initial;
enum class roomType { main_hall, exam_hall, lecture_room, tutorial_room, design_studio, meeting_room };
struct node {
string data;
int id;
int capacity;
int year, month, day;
int deleteDate;
roomType type;
node* next;
};
node* front;
node * tail;
room() {
length=0;
initial=1;
front = NULL;
tail = NULL;
}
bool isFull () { return length>=20; }
// Add Rooms
void room::addRoom() {
system("cls");
if (isFull()) {
cout<<" No more than 25 rooms are allowed\n"<<endl;
return;
}
cout << "Enter the capacity" << endl;
int a;
cin >> a;
node* temp = new node();
temp->data = "Not Reserved";
temp->id = initial;
temp->year = 0;
temp->month = 0;
temp->day = 0;
temp->deleteDate = 0;
initial++;
temp->capacity = a;
temp->next = NULL;
if (front == NULL && tail == NULL)
{
front = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
cout << "Choose The type" << endl;
cout << "1- Main Hall \t 2- Lecture Room \t 3- Exam Hall \t 4- Meeting Room \t 5- Design Studio \t 6- Tutorial Room" << endl;
int t;
cin >> t;
if (t == 1)
{
temp->type = roomType::main_hall;
}
else if (t == 2)
{
temp->type = roomType::lecture_room;
}
else if (t == 3)
{
temp->type = roomType::exam_hall;
}
else if (t == 4)
{
temp->type = roomType::meeting_room;
}
else if (t == 5)
{
temp->type = roomType::design_studio;
}
else if (t == 6)
{
temp->type = roomType::tutorial_room;
}
else {cout << "Wrong Input!" << endl;}
length++;
cout<<"\n Successfully Created!\n\n";
system("pause");
save();
}
void reserveRoom()
{
system("cls");
show();
cout << "=============================================================" << endl;
cout << "Enter the room ID you want to Book !" << endl;
int id;
cin >> id;
node* tmp = front;
while (tmp != NULL) {
if (tmp->id == id) {
if(tmp->data == "Not Reserved"){
tmp->data = "Reserved";
int y,m,d;
cout << "Enter the year " << endl;
cin >> y;
cout << "Enter the month " << endl;
cin >> m;
cout << "Enter the day " << endl;
cin >> d;
tmp->year = y;
tmp->month = m;
tmp->day = d;
tmp->deleteDate = y+m+d;
cout << "Room Reserved!" << endl;
}
else{
cout << "This room has been reserved!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
}
}
tmp = tmp->next;
}
system("pause");
room::save();
}
};
最佳答案
I know how to read a text file line by line and save it. but my problem is how to read text file word by word.
This is the text file I have:
1-Reserved-2018-12-23-Lecture Room-13 2-Reserved-2018-11-34-Tutorial Room-15 3-Not Reserved-0-0-0-Design Studio-18 4-Reserved-2018-11-16-Lecture Room-14 5-Not Reserved-0-0-0-Exam Hall-18
从我上面的评论继续,每当你面临用单词之间的分隔符分隔一行中的单词时,标准方法是将每一行放入 string
中。与 getline
然后创建一个 stringstream
从将每个单词读入 string
的行使用 getline
带有指定的分隔符。
为什么用 getline
读取一行并阅读 stringstream
与 getline
又是?
答案:线控。
虽然您可以使用 getline
直接从文件中读取内容和一个分隔每个单词的定界符,你怎么知道一行结束和下一行开始的时间?当您指定要与 getline
一起使用的分隔符时, getline
将读取直到找到分隔符或输入结束或 str.max_size
字符已被读取。参见 cppreference.com - std::getline .所以行尾'\n'
没有特殊意义在这种情况下。
但是,如果您将整行读入 string
然后创建一个 stringstream
从该行中,您知道您只能读取到行尾,因为这将在输入时触发文件结束条件。因此,即使您使用的是 getline
带有分隔符,它现在只能读取到行尾。
一个使用这种方法并使用您的数据文件的简短示例将展示如何将每一行分成单词,然后您可以将这些单词添加到列表的每个节点,例如
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main (int argc, char **argv) {
std::string line; /* string to hold each line */
if (argc < 2) { /* validate at least 1 argument given */
std::cerr << "error: insufficient input.\n"
"usage: " << argv[0] << " filename\n";
return 1;
}
std::ifstream f (argv[1]); /* open file */
if (!f.is_open()) { /* validate file open for reading */
perror (("error opening file " + std::string(argv[1])).c_str());
return 1;
}
while (getline (f, line)) { /* read each line into line */
std::string word; /* string to hold words */
std::stringstream s (line); /* create stringstream from line */
while (getline (s, word, '-')) /* read hyphen separated words */
std::cout << word << '\n'; /* output words */
std::cout << '\n'; /* tidy up with newline between data */
}
}
示例输入文件
$ cat ../dat/hyphenstr.txt
1-Reserved-2018-12-23-Lecture Room-13
2-Reserved-2018-11-34-Tutorial Room-15
3-Not Reserved-0-0-0-Design Studio-18
4-Reserved-2018-11-16-Lecture Room-14
5-Not Reserved-0-0-0-Exam Hall-18
示例使用/输出
请注意,上面的代码只是输出一个额外的 '\n'
每行分隔的单词之间。您将编写逻辑(可能使用计数器,例如 stoi
用于任何需要的转换)将值转换为整数值并将每个值存储在适当的字段中。
$ ./bin/getline_hyphen ../dat/hyphenstr.txt
1
Reserved
2018
12
23
Lecture Room
13
2
Reserved
2018
11
34
Tutorial Room
15
3
Not Reserved
0
0
0
Design Studio
18
4
Reserved
2018
11
16
Lecture Room
14
5
Not Reserved
0
0
0
Exam Hall
18
你也可以去掉每一行的分隔符,创建一个单独的stringstream
没有连字符并使用 >>
读取和转换每个节点的值。 (这第二种方法留给你)
检查一下,如果您还有其他问题,请告诉我。
关于C++从文本文件中读取字符串并逐字保存到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54273717/