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)
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?
EDIT: I tried using the suggestion that Thiruvalluvar provided, but I just could not get it to work. However, I did switch to sscanf and that is working quite well almost...
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*/
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?