在 C++ 中使用 fstream 在文件中查找字符串

finding a string in a file using fstream in c++

这是我的代码

/*
    Asks the user for their ID, depending on the ID depends on the results. It either goes to maintanance
    or it asks the user to return DVD's or check DVD's out and changes the stock of the DVD's.
    Cody Close
*/

#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
#include <string>

using namespace std;

void custID();
void sales();
void returns();
void discounts();
void maint();
void createAcc(string* filename, string* newID);
bool checkID(string* filename, string* search);

int main()
{
   //Declares all the variables for the program
   int mainID= 99959, menuChoice;
   bool close = false;
   bool done = false;
   string vidId;

   //Declares and input file and opens a file
   fstream inFile;
   inFile.open("dayin00.dat");

   do{
       do{
           cout << "accountID: " << endl;
           cin >> mainID;
           stringstream out;
           out << mainID;
           mainid = out.str();
           checkID("IDlist.txt", mainid);
       }while(mainid.length() < 5 || mainid.length() > 9);
           if(mainID!= 99959)
           {
               do
               {
                   cout << "MENU:" << endl;
                   cout << "(1)Purchase\n(2)Return\n(3)Exit" << endl;
                   cin >> menuChoice;
                   switch(menuChoice)
                   {
                   case 1:
                   case 2:
                   case 3:
                       done = true;
                   }
               }while(done == false);
           }else{
               maint();
           }

       close = true;
   }while(close == false);

   return 0;
}

void maint()
{
   int maintChoice;

   cout << "\n(1)Summary\n(2)Withdrawl\n(3)Close Down\n(4)Back to >main\n(0)Help" << endl;
   cin >> maintChoice;

   switch (maintChoice)
   {
       case 1:

       case 2:
       case 3:
       case 4:
       default:
           cout << "1 for summary, 2 for withdrawl, 3 to close down, 4 to >go back to main" << endl;
   }
}

void createAcc(string* filename, string* newID)
{
   fstream newFile;
   newFile.open(filename);
   newFile << newID;
}
void checkID(string* filename, string* ID)
{
   fstream infile;
   infile.open("IDlist.txt");
   string word;

   infile >> word;
   while (!infile.eof()){
       if(word == ID)
       {
           cout << "ID FOUND!" << endl;
       }else{
           createAcc(infile, ID);
       }
   }
}

文本文件只包含ID 99959,如何检查用户输入的ID是否已经存在于文本文件中,如果不存在,则进入createAcc(),建立一个新的使用用户输入的 ID 的帐户。

代码以读取模式打开带有用户 ID 的文件,逐行读取并尝试查找 ID。如果在文件中找不到 ID,则以写入模式打开文件并在文件中添加用户 ID。

#include <iostream>
#include <fstream>
#include <stdexcept>

void createAcc(const std::string& filename, const std::string& id)
{
    std::ofstream os(filename);
    if (os)
        os << id;
    else
        throw std::runtime_error("Open file error: " + filename);
}

bool isStringContainsID(const std::string& line, const std::string& id)
{
    if (line.find(id) == std::string::npos)
        return false;
    else
        return true;
}

bool isFileContainsID(const std::string& filename, const std::string& id)
{
    std::ifstream is(filename);
    if (!is)
        throw std::runtime_error("Open file error: " + filename);
    std::string line;
    while (is) 
    {
         std::getline(is, line);
         if (isStringContainsID(line, id))
             return true;
    }
    return false;
}

int main() {

    std::string id("99959");
    std::string file_name("IDlist.txt");

    if (isFileContainsID(file_name, id))
        std::cout << "ID FOUND!" << std::endl;
    else
        createAcc(file_name, id);

    return 0;
}

请注意,所有用户 ID 的字符串表示形式应具有相同的长度,否则代码可以在包含较大 ID 的文件中找到较短的 ID,并将较短的 ID 作为子字符串。