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

时间:2015-04-11 20:57:01

标签: c++ fstream

这是我的代码

/*
    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。

1 个答案:

答案 0 :(得分:1)

代码打开文件,其中用户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。