从txt文件读入FIFO队列进行处理

时间:2018-03-30 17:50:58

标签: c++ sorting queue text-files heapsort

我对如何使用队列以及如何实现它们感到困惑。我必须按顺序读取数据包,然后进入队列进行处理。一旦完成,我将不得不从队列中顺序读取数据包,并根据其VLAN标记和优先级编号将它们重定向到两个优先级队列之一,这是文本文件中的一个示例,但是大约有10000个它们。

55555555555555D507E34B68534300A0FF7538958100010000086461746162617365C704DD7B This is what the numbers mean

我遇到的问题是队列本身,我觉得我用错误的方式,我会感激一些帮助。

https://pastebin.com/B4ZG2RmL< - 这是标题

using namespace std;

struct packetItem
{
    string packet;
    int packetInt;
};

int main()
{
    packetItem newItem;
    ifstream myfile("packets.txt");
    int count = 0;
    int vlan = 0, priority = 0;
    const int v = 100;
    char sArray[v];



    const int size = 100;

    if (myfile.is_open())
    {
        //while (myfile.good()) 
        while (count < 10) // this 10 is here so it doesnt spam the output
        {
            myfile >> newItem.packet;
            myfile.get(); //remove return
            strcpy_s(sArray, newItem.packet.c_str());
            int x = 0;

            while (sArray[x] != '\0')
            {
                x++;
                string part(sArray.substr(48, 4));
            }
            cout << count + 1 << ". " << sArray << "\n\t" <<x << "\n";
            count++;
        }   
    }
    myfile.close();

    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:2)

C ++附带了队列数据结构的标准实现。 Take a look

以下是使用数据包的简单示例:

std::queue<packetItem> pack_queue();
packetItem item;

while (/* What ever condition you choose */) {
        stream >> item.packet;
        pack_queue.push(item);

        /* Process packet */       
}

/* When you want to get the top packet, then just do this */
packetItem front_packet = pack_queue.front();

我知道这是一个非常简短的例子,所以请看一下文档以便更好地掌握。

我希望这能回答你的问题。它有点广泛。

答案 1 :(得分:1)

好吧,我发现我们可以在您的代码中解决许多问题。有些很重要,有些很高兴。但是,让我们的明星:

  1. 你说过你希望你有排队&#34;。在这种情况下,我会说,使用std:queue(http://www.cplusplus.com/reference/queue/queue/),这将为您提供节省大量时间的机会,并使您的代码更清洁。

  2. 你说过,你希望你有许多优先排队的队列。我会说这只不过是std :: vector的std :: vector,所以最终会得到这样的结论:

  3. std::vector<std::queue<Frame>> queues;

    1. 然后你可以用一点结构包装它:
    2. //

      #include <iostream>
      #include <string>
      #include <vector>
      #include <queue>
      #include <experimental/optional>
      
      template <class T> class Scheduler {
           private:   std::vector<std::queue<T>> queues;
      
           public:
              Scheduler(int maxSize) {
                  queues = std::vector<std::queue<T>>(maxSize);
              };
      
              void schedule(int priority, const T& object) {
                  std::cout << "enqueue on queue #" << priority << std::endl;
                  queues[priority].push(object);
              };
              std::experimental::optional<T> take(int priority) {
                  std::experimental::optional<T> result;
                  if (!queues[priority].empty()) {
                      result = std::experimental::optional<T>(queues[priority].front());
                      queues[priority].pop();
                  }
                  return result;
      
              };
      };
      
      1. 所以用法很清楚:

         Scheduler<int> scheduler(5);
        
         scheduler.schedule(0, 10);
         scheduler.schedule(0, 14);
        
         for (std::experimental::optional<int> o = scheduler.take(0); (bool)(o) == true; o = scheduler.take(0)) {
             std::cout << "O " << o.value() << std::endl;
         }
        
      2. 关于队列的全部内容......但现在您需要准备一个描述数据的结构/类。类似的东西:

        class Frame {     std :: string preamble;     std :: string mac;     int vlanId;     /// ......等等 }

      3. 然后你必须将你的消息分成小部分,但不要使用char *(来自c语言),而是使用std :: string.substr(n,p);

        因此,假设您的代码中会出现类似内容:

        myfile >> line;
        Frame frame;
        frame.preamble = line.substr(0, LEN_OF_PREAMBLE);
        frame.mac = line.substr(LEN_OF_PREAMBLE, LEN_OF_MAC);
        //... and so one
        scheduler.push(frame.vlan, frame);