简体   繁体   English

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

[英]finding a string in a file using fstream in c++

This is my code 这是我的代码

/*
    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);
       }
   }
}

The text file only contains the ID 99959. How do I check if the ID the user types in already exists in the text file and if it doesn't, then it goes to createAcc(),setting up a new account using the ID that the user has entered. 文本文件仅包含ID99959。如何检查用户键入的ID是否已存在于文本文件中,如果不存在,则转到createAcc(),使用该ID设置新帐户用户已输入。

The code opens file with users ID in read mode, reads it line by line and tries to finde ID. 该代码以读取模式打开具有用户ID的文件,逐行读取它并尝试查找ID。 If ID not found in file, it opens file in write mode and add user ID in file. 如果在文件中找不到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;
}

Note that all users ID should have the same length in string representation, otherwise the code can find shorter ID in file that contains larger ID with shorter ID as sub-string. 请注意,所有用户ID的字符串表示形式都应具有相同的长度,否则代码可以在包含较大ID且文件ID较短的子字符串中找到较短的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM