c++ - 如何检查从键盘输入的特定整数值是否存在于C++文件的一行或多行中
问题描述
我有一个 C++ 课程的小项目,我一直在尝试检查文件中是否存在 STUDENT 类的数据成员的值(“ID”)。我尝试使用我在互联网上找到的一些函数将我正在搜索的整数值转换为字符串,然后使用 find 函数在文件的每一行中搜索它。
它可以工作,但是每当我检查文件中的一行时,它都会出错,因为 ID 值(例如“12”)与年龄的值(也是“12”)相同。这样做是因为年龄值在我的文件和字符串变量中的 ID 值之前(我无法更改它)。我不知道只在字符串中搜索 ID 的值。我使用函数“inputInfo”从键盘输入student1的成员值,并使用函数“checkID”检查文件中是否已经存在“ID”的值。此外,对于项目的另一个方面,我正在寻找一种方法来搜索同一文件中出现的 ID 和名称数据成员值(一旦它们已经被写入)。一种解决方案我'
#include<iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
int checkID(int idNumber)
{
string findID;
stringstream id_string;
id_string << idNumber;
findID = id_string.str();
int offset;
ifstream in;
in.open("Students.txt");
if(in.is_open())
{
string line;
while(getline(in, line))
{
if(offset = line.find(findID, 0)!= string::npos)
{
cout<<"The ID already exists. Insert a different ID!"<<endl;
return 0;
}
}
}
else
cout<<"File doesn't exist!"<<endl;
in.close();
}
class PERSON
{
protected:
string name;
string surname;
unsigned int age;
public:
void inputinfo()
{
cin>>name;
cin>>surname;
cin>>age;
}
outputinfo()
{
cout<<name<<endl;
cout<<surname<<endl;
cout<<age<<endl;
}
};
class STUDENT: public PERSON
{
int ID;
float marks_sum;
string belonging_class;
public:
inputInfo()
{
cout<<"Name:";
cin>>name;
cout<<"Surname:";
cin>>surname;
cout<<"Age:";
cin>>age;
do
{
cout<<"ID:";
cin>>ID;
}
while (checkID(ID)==0);
cout<<"Sum of marks:";
cin>>marks_sum;
cout<<"The belonging class:";
cin>>belonging_class;
}
void outputInfo()
{
cout<<name<<endl;
cout<<surname<<endl;
cout<<age<<endl;
cout<<ID<<endl;
cout<<marks_sum<<endl;
cout<<belonging_class<<endl;
}
friend std::ostream& operator << (std::ostream& os, const STUDENT& value )
{
os << value.name<<" "<<value.surname<<" "<<value.age<<" "<<value.ID<<" "<<value.marks_sum<<" "<<value.belonging_class<<std::endl;
return os;
}
};
STUDENT student1;
int writeInFile(STUDENT studentx)
{
ofstream os("Students.txt", ofstream::app);
os << studentx;
os.close();
}
int main()
{
int opt1, opt2;
char option;
do
{
cout<<"1 - Input data into file"<<endl<<"2 - Close program"<<endl;
cin>>opt1;
switch(opt1)
{
case 1:
do
{
cout<<endl;
cout<<"Choose one of variants"<<endl<<"1.Students"<<endl<<"2.Get back to main menu"<<endl;
cin>>opt2;
switch(opt2)
{
case 1:
do
{
cout<<"Do you wish to introduce a new student(Y/N)?";
cin>>option;
if(option!='N')
{
student1.inputInfo();
writeInFile(student1);
}
}
while (option!='N');
break;
}
}
while(opt2!=2);
break;
}
}
while(opt1!=2);
}
解决方案
#include <sstream>
using namespace std;
bool isUniqueID(ifstream& file, int id)
{
string id_string = to_string(id);
string currently_read_line;
// The position of the searched key. So, in this case,
// only the 3rd value will be tested (starting from 0).
// John Doe 23 456
// | | | |
// 0 1 2 3 (the id)
int offset = 3;
while (getline(file, currently_read_line))
{
istringstream ss(currently_read_line);
string current_entry;
int counter = 0;
while (ss >> current_entry) {
if (current_entry == id_string && counter == offset) {
cout << "The Id already exists." << endl;
return false;
}
counter++;
}
}
// No match found
cout << "The ID does not exist yet." << endl;
return true;
}
请注意:
- 只需将打开的文件传递给函数。该文件只打开一次,而不是每次您要检查 ID 时都打开它。
- 这需要在 -std=c++11 中编译(用于 to_string 转换)
[更新]
offset 变量告诉函数要测试什么值。一种更一致的方法是将数据格式化为每个学生条目都有一个键/值。它虽然工作。
推荐阅读
- jackrabbit - 长耳兔聚类
- javascript - 父项也切换子项
- hadoop - 如何强制datanode和resourcemanager使用IP而不是主机名?
- awk - 给定主机名,从 ~/.ssh/config 获取主机
- java - 如何刷新/更新@Autowired EurekaClient
- c# - iTextsharp - c# - PDF 不可读
- sqlite - 插入 SQlite 数据库时出现列名错误
- node.js - 使用 NodeJs 获取 Google Datastore 实体
- sql - 错误检索 SQLserver 表中的值位字段
- javascript - 将 Amazon Cognito 的授权代码授予与无服务器/单页 Web 应用程序一起使用