最佳读取文本文件在C解析数据的方法吗? [英] Best way to read a parse data from text file in C?
问题描述
我的工作,从文本文件读取数据涉及的转让和解析这些数据在不同的阵列。例如,我的文本文件的一部分看起来如下:
I am working on an assignment that deals with reading data from a text file, and parsing that data to various arrays. For example, a portion of my text file looks as follows:
arbrick pts/26 141.219.210.189 Thu Mar 29 11:23 - 11:24 (00:00)
rjmcnama pts/27 141.219.205.107 Thu Mar 29 11:02 still logged in
ajhoekst pts/26 99.156.215.40 Thu Mar 29 10:59 - 11:08 (00:08)
eacarter pts/31 141.219.162.145 Thu Mar 29 10:50 - 10:51 (00:00)
kmcolema pts/31 141.219.214.128 Thu Mar 29 09:44 - 09:47 (00:03)
我需要将数据解析为以下阵列:用户ID,终端,IP地址和事件时间。我怎样才能做到这一点考虑到不存在的列之间的空白一个一致的金额是多少?
I need to parse the data into the following arrays: user id, terminal, ip address, and event times. How can I do this considering that there isn't a consistant amount of white space between the columns?
编辑:
我试图用Thiruvalluvar提供的建议,但我可能只是无法得到它的工作。不过,我并切换到sscanf和正在工作相当不错的几乎 ...
while(!feof(myfile)) {
fgets(buffer, 256, myfile);
sscanf(buffer, "%s %s %s %s", user_id[i], terminal_id[i], ip_addr[i], events[i]);
} /*End while not EOF*/
什么是工作,是USER_ID,TERMINAL_ID和阵列的ip_addr。然而,事件数组不完全工作作为呢。由于该事件数组是包含空格的字符串,我该如何使用的sscanf到的的缓存的剩余部分添加到事件中的数组?
What is working, is the user_id, terminal_id, and ip_addr arrays. However, the events array isn't working perfectly as of yet. Since the events array is a string that contains white space, how can I use sscanf to add the remainder of the buffer to the events array?
推荐答案
我认为,问题的实部是如何strore他们只有4个阵列。例如:
I think, the real part of the question is how to strore them in only 4 arrays. E.g.:
arbrick pts/26 141.219.210.189 Thu Mar 29 11:23 - 11:24 (00:00)
标记化与空白此行是布莱恩给许多字符串。但是,我们只在分裂整个生产线到只有4行,不多说更感兴趣。
Tokenizing this line with whitespace is goin to give many strings. But we are only interested in splitting the entire line into only 4 lines, not more than that.
解决方案:
-
使用
与fgets读行()
。
标记化这使用的strtok()
或 strtok_r()
(线程安全的)有空白作为分隔符。
Tokenize it using strtok()
or strtok_r()
(for thread-safe) with whitespace as delimiter.
阅读一日3串入数组:USER_ID,TERMINAL_ID和IP_ADDRESS
Read the 1st 3 strings into the arrays: user_id, terminal_id and ip_address
存储(和附加)字符串的其余部分到数组事件
。
Store ( and append) the rest of strings into the array events
.
int i = 0;
int line_index = 0;
char *p;
while(...) //loop to read the file
{
fgets(line);
p = strtok(line, " ");
i=0;
while(p!=NULL)
{
if(i==0) strcpy(user_id[line_index], p);
if(i==1) strcpy(terminal_id[line_index], p);
if(i==2) strcpy(ip_addr[line_index], p);
else strcat(events[line_index], p); //anything else goes into array events
i++;
}
line_index++;
} //end of file-reading loop.
这篇关于最佳读取文本文件在C解析数据的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!