[英]Extracting portion of a proc/status linux file
First, I'm totally beginner in linux and C language, I can't relate C to c++ or java much when it comes to strings!. 首先,我完全是Linux和C语言的初学者,在字符串方面,我不能将C与c ++或java的联系太多! I'm using Fedora16 - linux - and I want to read a proc/[pid]/status file to get specific information from it, such as PPID and state, then I should print these info on the screen - command line terminal-.
我使用的是Fedora16-linux-我想读取proc / [pid] / status文件以从中获取特定信息,例如PPID和状态,然后将这些信息打印在屏幕上(命令行终端)。 This must be done by writing ac script in gedit.
这必须通过在gedit中编写ac脚本来完成。 My only issue is that I'm new to c, and dealing with strings in c seems very frustrating to me!
我唯一的问题是我是c语言的新手,在c语言中处理字符串似乎让我很沮丧! I have already opened the file and view it on my terminal by executing the c file.
我已经打开文件并通过执行c文件在我的终端上查看它。 Is there any possible way to store the whole content in one string variable and then I can tokenize it and store the chunks of data as in string array not char array, then I know where my wanted data is in the array and I can access it?
有没有办法将整个内容存储在一个字符串变量中,然后我可以对其进行标记化并将数据块存储在字符串数组而不是char数组中,那么我知道所需数据在数组中的什么位置,就可以访问它了?
Here is my code though 这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
const char line[200];
const char junk[200];
FILE *file = fopen("/proc/14/status", "r");
// while not end of the file
while(!feof(file)) {
fscanf(file,"%s",line); //Get text into line array
printf("%s\n", line);
//fscanf(file,"%[ \n\t\r]s",junk); //Remove any 'white space' characters
}//end while
fclose(file);
}
Output On Terminal: 端子输出:
There are two things wrong initially 最初有两件事是错误的
line
should not be const
. line
不应该是const
。 while (!feof(file))
is almost always wrong. while (!feof(file))
几乎总是错误的。 The fix involves doing something like 解决方法包括执行类似的操作
while (fscanf(file, "%199s", line) == 1)
which will loop until there is no more data and will prevent overflowing line
. 这将循环直到没有更多数据为止,并防止
line
溢出。
This will fix something, the other thing is rather complicated, first try using fgets()
instead if fscanf()
, it will consume lines from the file including the '\\n'
and the embedded white spaces 这将解决问题,而另一件事则相当复杂,请首先尝试使用
fgets()
而不是fscanf()
,它将消耗文件中的行,包括'\\n'
和嵌入式空格
while (fgets(line, sizeof(line), file) != NULL)
then you can try sscanf()
checking it's return value to ensure that it succeeded. 然后您可以尝试
sscanf()
检查它的返回值以确保成功。
From the content of /proc/self/status
you can see that strchr()
would do a good job in splitting the lines in the interesting parts. 从
/proc/self/status
的内容中,您可以看到strchr()
可以很好地将行分成有趣的部分。
This is an example: 这是一个例子:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int
main(void)
{
FILE *file;
char line[100];
file = fopen("/proc/self/status", "r");
if (file == NULL)
return -1; /* Failure to open /proc/self/stat -- very unlikely */
while (fgets(line, sizeof(line), file) != NULL)
{
char *tail;
char *key;
char *value;
tail = strchr(line, '\n');
if (tail != NULL)
*tail = '\0'; /* remove the trailing '\n' */
tail = strchr(line, ':');
if (tail != NULL)
{
tail[0] = '\0';
key = strdup(line);
if (key == NULL)
continue;
tail += 1;
while ((tail[0] != '\0') && (isspace((int) tail[0]) != 0))
tail++;
value = strdup(tail);
if (value != NULL)
{
fprintf(stderr, "%s --> %s\n", key, value);
/* You could do something now with key/value */
free(value);
}
free(key);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.