如何解析以下文件? [英] How to parse the following file ?

查看:86
本文介绍了如何解析以下文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析以下代码:-

How to parse the following code:-

COMMAND     PID       USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
init          1        ???  exe       ???                ???       ???        ??? /init
init          1        ???    0       ???                ???       ???        ??? /dev/__null__ (deleted)
init          1        ???    1       ???                ???       ???        ??? /dev/__null__ (deleted)
init          1        ???    2       ???                ???       ???        ??? /dev/__null__ (deleted)
init          1        ???    3       ???                ???       ???        ??? /dev/__kmsg__ (deleted)
init          1        ???    4       ???                ???       ???        ??? /dev/__properties__ (deleted)
init          1        ???    5       ???                ???       ???        ??? socket:[257]
init          1        ???    6       ???                ???       ???        ??? socket:[259]
init          1        ???    7       ???                ???       ???        ??? socket:[260]
init          1        ???  mem       ???              00:01         0         19 /init
init          1        ???  mem       ???              00:01     90112         19 /init
<snip>




我想将pid(即pid列下的值)获取到数组中,但是由于您可以看到pid列下的整数重复,所以我只希望每个整数值一次....然后返回包含以下内容的数组函数




I want to get the pid(that is the value under pid column) to in an array,but as u can see the integers under pid column repeats,i want each integer value only once....and then return the array that contains the integers(pid) from the function

推荐答案

伪代码中的integers(pid):

pseudo code :

1. read line.
2. use strtok (for example or any other parsing mechanism) to parse the line;
3. get the 2nd column value
4. insert the value into a std::set<int>
5. goto 1.
6. copy the std::set into an array or a std::vector <int>



添加了std :: set
的用法



added usage of std::set

std::set<int> pidSet;
while (readline)
{
  // extract pidValue from line.
  pidSet.insert(pidValue);
}




您可以将值直接放入std :: vector并使用算法清除重复的值(我敢肯定,STL中有内置的功能可以做到这一点).




you could put the value directly into a std::vector and use an algorithm to clear the duplicate values (I''m certain there is something buildin STL to do just that).


如果您只需要pid,可以使用std :: map<>以确保您拥有一套独特的价值.

If you only want the pid, you can use a std::map<> to insure you have a unique set of values.

std::map<int,> PidMap;

bool AddPid(int NewPid)
{
  // Check to see if the pid is already in the map
  if (PidMap.find(NewPid) != PidMap.end();
     return false;

  PidMap.insert(std::pair<int,>(NewPid, true));

  return true;
}

bool ProcessPids(void)
{
   std::map<int,>::iterator Iter = PidMap.begin();
   std::map<int,>::iterator End  = PidMap.end();
   for (; Iter != End; Iter++)
   {
       int Pid = (*Iter).first;

       // Do stuff with the Pid here.
   }

   return true;
}


您上次遇到这个问题时没有学到什么吗?
请记住,(实际上)所有复杂的问题都可以分解为一系列较小,较简单的问题.

通常,重要的步骤是确定原始问题可以在哪里/如何分解为较小的任务.该技能会随着经验的提高而提高,尽管它也是解决问题的一种必要方法,以最大程度地减少解决和调试问题所需的时间.


将这种技术应用于您当前的问题可能是一个值得练习的方法.

需求/观察摘要
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.我们需要从文本文件的每一行中提取值
2.这些值可能是重复的-我们只想存储每个值的单个实例.
3.由于值可能是重复的,所以我们没有直接的方法来确定唯一值的数量,但是-我们知道它必须小于(或等于)输入文件中的行数.

选择:
1.重复值的管理
---------------------------------
A.仅维护一个不同值的列表.这将需要检查许多与我们处理的输入文件的每一行的所有先前值相同的值.需要将第1行与0个先前值进行比较. 1000行将需要与最多999行进行比较-如果较早发现PID,则要少一些,如果没有找到PID,则要比较999.

B.保留所有值,进行排序,将找到的每个不同值的1个实例复制到新列表中.

C.使用将为您处理重复数据的stl数据结构(std :: map会出现在我的脑海中,尽管我从未使用过)


2.提取数据项
---------------------------
A.将行标记化,提取并使用所需的标记-更复杂&健壮的
B.直接提取所需的元素-更简单,更容易出错.



这是水壶沸腾时我砸碎的一些代码.
它会
(a)计算文件中的行数
(b)显示每行的COMMAND和PID列

对于上面列出的两种选择,您都必须确定自己的首选方法.
祝你好运!

Did you not learn something from the last time we went through this issue?
Remember, (virtually) all complex problems can be broken down into a series of smaller, simpler ones.

Often the important step is to identify where/how the original problem can be broken down into smaller tasks. This skill improves with experience, though it is also a necessary way to look at the problem in order to minimize the time taken to solve it and debug it.


Applying this technique to your current question may be a worth exercise.

Summary of requirements/observations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. We need to extract the values from each line of a text file
2. These values may be duplicated - we only want to store a single instance of each value.
3. Since values may be duplicated, we have no direct way of determining the number of unique values, HOWEVER - we do know that it must be less than (or equal to) the number of lines in the input file.

Choices:
1. Management of duplicate values
---------------------------------
A. only maintain a list of distinct values. This will require examining as-many-as all previous values for each line of the input file that we process. Line 1 will need to be compared against 0 previous values. Line 1000 will need to be compared to up to 999 lines - less if the PID is found early, 999 compares if it isn''t found.

B. retain all values, sort, copy 1 instance of each distinct value found to a new list.

C. Use a stl datastructure that will take care of duplicates for you (std::map comes to mind, though I''ve never used it)


2. Extraction of data items
---------------------------
A. tokenize the line, extract and use the tokens that you need - more complex & robust
B. extract the required elements directly - simpler and more error-prone.



Here''s some code I smashed together while the kettle was boiling.
It will
(a) count the number of lines in the file
(b) display the COMMAND and PID columns of each line

You''ll have to work out your preferred method for each of the 2 choices I''ve outlined above.
Good luck!

#include <cstdio>

int countTextFileLines(char *filename)
{
    int i=0;
    char lineBuffer[1024];
    FILE *fp = fopen(filename, "rt");
    while (fgets(lineBuffer, 1024, fp))
        i++;
    fclose(fp);
    return i;
}

void displayPIDcolumn(char *filename)
{
    int pID;
    char lineBuffer[1024], cmdBuffer[20];
    FILE *fp = fopen(filename, "rt");
    while (fgets(lineBuffer, 1024, fp))
    {
        sscanf(lineBuffer, "%9s %5d", &cmdBuffer, &pID);
        printf("CMD: %s   PID: %d\n", cmdBuffer, pID);
    }
}

int main()
{
    char *filename = "topListing.txt";
    printf("%s has %d lines.\n", filename, countTextFileLines(filename));
    displayPIDcolumn(filename);

    return 0;
}


这篇关于如何解析以下文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆