C语言 解析文本文件的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5211028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
easy way to parse a text file?
提问by Blackbinary
I'm making a load balancer (a very simple one). It looks at how long the user has been idle, and the load on the system to determine if a process can run, and it goes through processes in a round-robin fashion.
我正在制作一个负载平衡器(一个非常简单的)。它查看用户空闲了多长时间,以及系统上的负载以确定进程是否可以运行,并以循环方式遍历进程。
All of the data needed to control the processes are stored in a text file. The file might look like this:
控制过程所需的所有数据都存储在一个文本文件中。该文件可能如下所示:
PID=4390 IDLE=0.000000 BUSY=2.000000 USER=2.000000
PID=4397 IDLE=3.000000 BUSY=1.500000 USER=4.000000
PID=4405 IDLE=0.000000 BUSY=2.000000 USER=2.000000
PID=4412 IDLE=0.000000 BUSY=2.000000 USER=2.000000
PID=4420 IDLE=3.000000 BUSY=1.500000 USER=4.000000
This is a university assignment, however parsing the text file isn't supposed to be a big part of it, which means I can use whatever way is the quickest for me to implement.
这是一项大学作业,但是解析文本文件不应该是其中的重要部分,这意味着我可以使用任何对我来说最快的方式来实现。
Entries in this file will be added and removed as processes finish or are added under control.
此文件中的条目将在进程完成或在控制下添加时添加和删除。
Any ideas on how to parse this?
关于如何解析这个的任何想法?
Thanks.
谢谢。
回答by Matteo Italia
As far as just parsing is concerned, something like this in a loop:
就解析而言,循环中是这样的:
int pid;
float idle, busy, user;
if(fscanf(inputStream, "PID=%d IDLE=%f BUSY=%f USER=%f", %pid, &idle, &busy, &user)!=4)
{
/* handle the error */
}
But as @Blrfl pointed out, the big problem is to avoid mixups when your application is reading the file and the others are writing to it. To solve this problem you should use a lock or something like that; see e.g. the flocksyscall.
但正如@Blrfl 指出的那样,当您的应用程序正在读取文件而其他应用程序正在写入文件时,最大的问题是要避免混淆。要解决这个问题,您应该使用锁或类似的东西;参见例如flock系统调用。
回答by Greg
Here is a code that will parse your file, and also account for the fact that your file might be unavailable (that is, fopenmight fail), or being written while you read it (that is, fscanfmight fail). Note that infinite loop, which you might not want to use (that's more pseudo-code than actual code to be copy-pasted in your project, I didn't try to run it). Note also that it might be quite slow given the duration of the sleep there: you might want to use a more advanced approach, that's more sort of a hack.
这是一个将解析您的文件的代码,并说明您的文件可能不可用(即fopen可能会失败)或在您阅读时正在写入(即fscanf可能会失败)这一事实。请注意,您可能不想使用无限循环(与实际代码相比,要复制粘贴到您的项目中的伪代码更多,我没有尝试运行它)。还要注意,考虑到那里的睡眠时间,它可能会很慢:您可能想要使用更高级的方法,这更像是一种黑客攻击。
int pid;
float idle, busy, user;
FILE* fid;
fpos_t pos;
int pos_init = 0;
while (1)
{
// try to open the file
if ((fid = fopen("myfile.txt","rw+")) == NULL)
{
sleep(1); // sleep for a little while, and try again
continue;
}
// reset position in file (if initialized)
if (pos_init)
fsetpos (pFile,&pos);
// read as many line as you can
while (!feof(fid))
{
if (fscanf(fid,"PID=%d IDLE=%f BUSY=%f USER=%f",&pid, &idle, &busy, &user))
{
// found a line that does match this pattern: try again later, the file might be currently written
break;
}
// add here your code processing data
fgetpos (pFile,&pos); // remember current position
pos_init = 1; // position has been initialized
}
fclose(fid);
}
回答by Robert S. Barnes
Use fscanfin a loop. Here's a GNU C tutorial on using fscanf.
在循环中使用fscanf。这是关于使用fscanf的 GNU C 教程。
/* fscanf example */
#include <stdio.h>
typedef struct lbCfgData {
int pid;
double idle;
double busy;
double user;
} lbCfgData_t ;
int main ()
{
// PID=4390 IDLE=0.000000 BUSY=2.000000 USER=2.000000
lbCfgData_t cfgData[128];
FILE *f;
f = fopen ("myfile.txt","rw+");
for ( int i = 0;
i != 128 // Make sure we don't overflow the array
&& fscanf(f, "PID=%u IDLE=%f BUSY=%f USER=%f", &cfgData[i].pid,
&cfgData[i].idle, &cfgData[i].busy, cfgData[i].user ) != EOF;
i++
);
fclose (f);
return 0;
}

