Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:45:02+00:00 2026-06-01T04:45:02+00:00

I am working on an assignment that deals with reading data from a text

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:45:03+00:00Added an answer on June 1, 2026 at 4:45 am

    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)    
    

    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.

    Solution:

    1. Read the line using fgets().

    2. Tokenize it using strtok() or strtok_r() (for thread-safe) with whitespace as delimiter.

    3. Read the 1st 3 strings into the arrays: user_id, terminal_id and ip_address

    4. 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.
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal of the assignment that I'm currently working on for my Data Structures
Here is an example of a parsed xml file I'm working with that tabs
I'm currently working through an assignment that deals with Big-O and running times. I
I am currently working on an assignment that deals with RMI. Essentially, it is
I'm working on an assignment that involves writing a program to process data (calculate
I am working on a school assignment that requires me to be able to
I'm working on a homework assignment (a project), for which one criterion is that
I am working on an assignment that is giving me grief. I am supposed
I am working on a short java assignment that I have been set. The
I'm working on a Java assignment that has to be done using AWT. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.