[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parsing / Computations On A Records File - Help

Posted on 2006-06-18
4
Medium Priority
?
242 Views
Last Modified: 2010-04-01
Hi,

   I am new to C++ and am not very fimiliar with it.  I have used Java before, and many of the things I would utilize in Java are not available to me, so I am in need of some assistance.

   If I have txt file that has one record on each line, made up of three fields, seperated by spaces.  For example:

"01/01/01"   "a command"   "a resource"

all without "" marks, which I am just using to keep the fields separate for the post.

I have to do the following computations on them:

- avg number of requests per day.
- avg number of each individual command per day.
- avg number of requests per resource per day.


Any help of how to go about doing this is appreciated.  Thanks.
0
Comment
Question by:SuperG03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 200 total points
ID: 16931723
>> "01/01/01"   "a command"   "a resource"
01/01/01   a command   a resource
If the fields are not delimited by "", then how do you say there are 3 records, not 5.
How do you exactly read them in JAVA.

Can you give a more complete sample of data
And if possible post your code in JAVA.

So, that we can gve you alternative functions in C++.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 16933340
>>>> Can you give a more complete sample of data

As rajeev told the fields "a command" and "a resource" definitively may not contain space characters. Assuming that is the case you would/could solve like that:

struct Record
{
     string date;
     string command;
     string resource;
};

    ...
    vector<Record> records;
    ifstream ifs("file.dat);
    string line;
    while (getline(ifs, line))
    {
          Record rec;
          int pos1 = line.find(' ');
          if (pos1 != 8)  // assuming mm/dd/yy
              continue;   // error
          int pos2 = line.find(' ', pos1+1);
          if (pos2 == string::npos)
                continue;  // error
          //                        year                    day                      month
          rec.date = "20" + line.substr(6, 2) + line.substr(0, 2) + line.substr(3, 2);
          rec.command = line.substr(pos1+1, pos2-pos1);
          rec.resource  = line.substr(pos2+1);
          records.push_back(rec);
    }

After that all records are in the dynamic array. To get the statistics you should sort the records array either by date, command, resource or a combination of both.

You could do that by providing a built-in operator< that can be switched by setting an appropriate class member:

struct Record
{
     string date;
     string command;
     string resource;

     enum SortMode { DATE, COMMAND, RESOURCE, DATE_COMMAND, DATE_RESOURCE };
     static SortMode sortMode;

      bool operator<(const Record& r) const
      {
           switch(sortMode)
           {
           case DATE: return date < r.date;
           case COMMAND: return command < r.command;
           case RESOURCE: return resource < r.source;
           case DATE_COMMAND: return date < r.date || (date == r.date && command < r.command;
           case DATE_RESOURCE: return date < r.date || (date == r.date && resource < r.resource;
           }
      }
      static void SetSortMode(SortMode smode) { sortMode = smode; }
     
};

// in some cpp initialization of the static member

Record::SortMode Record::sortMode = DATE;


With that you could sort the array from above, e. g. by

     
          Record::SetSortMode(Record::DATE_COMMAND);

          sort(records.begin(), records.end());

After sorting you easily can make the statistics by iterating the (sorted) array and watching the changes.

Regards, Alex
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question