Solved

Need Help for the C++ program

Posted on 2004-10-05
8
211 Views
Last Modified: 2010-04-02
I have a 1000+ lines data record file - newevent.txt. The content:

28/09/04 08:11:08        BYP     BYP COMMON T/C ALARM      ...  UCOS  Normal
28/09/04 08:11:08        BYP     BYP COMMON T/C ALARM      ..N  UCOS  Alarm
28/09/04 08:11:09        MAT2    MAT 33 HSR3 CB HGK0 O/CL  ...  Open         Issued by WS02
28/09/04 08:11:09        MAT2    MAT 33 HSR3 CB HGK0 O/CL  ..N  UCOS  Transit00
28/09/04 08:11:09        MAT2    MAT 33 HSR3 CB HGK0 O/CL  ...  Cmd COS to Open by WS02
28/09/04 08:11:09        BYP      BYP COMMON T/C ALARM       ...  UCOS  Normal
28/09/04 08:11:09        BYP      BYP COMMON T/C ALARM       ..N  UCOS  Alarm
28/09/04 08:18:49        DES1    DES33 MER CB H9L5   P/WP    ..N  UCOS  Operated
28/09/04 08:18:49        DES1    DES33 MER CB H9L5   O/CL    ..N  UCOS  Transit00
28/09/04 08:18:49        DES1    DES33 MER CB H9L5   O/CL   ..N  UCOS  Open
28/09/04 08:18:49        DES1    DES33 MER CB H9L5   P/WP   ...  UCOS  Normal

I need to have a C++ program that can:
1) open and read the content of the file - newevent.txt
2) check for "Open" and save the data record name e.g. line 2 data record name is "MAT 33 HSR3" ("Open" always from position 73 to 76, data record name always from position 36 to 46).
3) check in the 3 records above and below the data record with "Open" for "Operated". If found, compare the data record name with the data record name with "Open". If data record name matches each other, save the name in the file - filteredevent.txt ("Operated" always at position 73 to 80). Of course, at the begin and end of file, 3 records each above and below can be less or 0.

Kindly please help!!

Thanks.

0
Comment
Question by:ccjong
  • 5
  • 3
8 Comments
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Open a std::ifstream. Read lines into std::string, using getline. Keep a std::vector<string> of 7 lines read (use a round robin - to maintain them e.g. [lines_read % 7].

Ignore the start and end of this process for the moment. Conceptually it is easiest to go back to them, when you've got the middle bit right.

Look at the relevant part of the 4th string index [(lines_read-3) % 7] in the vector, using std::basic_string<>::substr to find an "Open". If you've got an "Open" look at the 1st 3 and the last 3 for "Operated".

Then read another string into the vector at position [lines_read % 7].

You'll need to handle the start and end, once you've got the middle bit working.

Let me know if any of this needs additional explanation.
0
 

Author Comment

by:ccjong
Comment Utility
Really appreciate if you can show me with the C++ codes.

Thanks.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Tell me which bit you are stuck on.

If you want me to write the application for you, you'll find that I suddenly go mercenary on you and would want to charge a fee to keep me in Chapagne and caviar. It's how I earn my living.

You may find that there is a more altruistic expert hereabouts, who is willing to do this gratis, but the best way to go about this through EE is to have a go yourself and use the collaborative process to find your way through the assignment.
0
 

Author Comment

by:ccjong
Comment Utility
This is the 2nd day I am learning C++ programming. And I really have difficult time trying to understand C++.

I have a string S1, and if I cout<<S1<<endl; I can see the whole string. If I cout<<S1[0]<<endl; I can see the 1st character. BUT, if I

string check;
check=S1[0]+S1[1]+S1[2]+S1[3];
cout<<check<<endl;

I can see 1 strange character.

I need to check "Open" by combine S1[70]+S1[71]+S1[72]+S1[73], please tell me any other way.

Thanks.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
Comment Utility
Use substr to look at substrings.

> This is the 2nd day I am learning C++ programming

If your boss has assigned you this, you've been set a project which will put you off C++ for life. It is much too tough for day 2!! I've been writing C++ for 12 years and I don't find this kind of thing easy.

It looks like a commercial application, so I'll give you some code. Here's a full working program, which I'd normally charge an arm and a leg for, being a commercially motivated kinda fellow.

If you want to learn C++, I recommend working from tutorials or primers. If you need work like this done for commercial purposes, I recommend employing the services of someone who has had time to work through tutorials and primers.

--------8<--------
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

vector<string> round_robin(7);
void check(int working_index,ostream& os);

int main(int argc,const char *argv[])
{
      // Usage: first parameter is input file and second is output file
      if (argc != 3) return (cerr << "Usage: " << *argv << " {input} {output}\n"),1;
      ifstream fin(*++argv); if (!fin) return (cerr << "Error unable to open input file " << *argv << '\n'),1;
      ofstream fout(*++argv); if (!fout) return (cerr << "Error unable to open output file " << *argv << '\n'),1;

      string line;
      int line_count = 0;

      // Load new line into round robin and compare 4th string
      while (getline(fin,line)) {

            // Index in the round robin to read into
            const input_index = line_count%7;

            // Read new line into the round robin
            round_robin[input_index] = line;

            // Index in the round robin for 4th string to work with
            const working_index = (line_count+7+3)%7;

            // Check the stringh at the working index
            check(working_index,fout);

            // Adjust line count
            ++line_count;
      }

      // Special handling for last three lines (already read)
      for (int i = 0;i < 3;i++) {

            // Index in the round robin to erase
            const input_index = (line_count+i)%7;

            // Remove input line from the round robin
            round_robin[input_index].clear();

            // Index in the round robin for 4th string to work with
            const working_index = (line_count+7+3+i)%7;

            // Check the string at the working index
            check(working_index,fout);
      }

}

const int OpenIndex = 73-1;            // Adjust?
const int OpenLength = 4;

const int OperatedIndex = 73-1;            // Adjust?
const int OperatedLength = 8;

const int RecordNameIndex = 36-1;      // Adjust?
const int RecordNameLength = 11;

// Check string at the specified working index in the round_robin
void check(int working_index,ostream& os)
{
      // Work with the 4th string in the round robin
      const string& working_line = round_robin[working_index];

      // Working line long enough?
      if (working_line.size() > OpenIndex) {

            // Look for "Open"
            if (working_line.substr(OpenIndex,OpenLength) == "Open") {

                  // Read off the record name
                  const string& recordName = working_line.substr(RecordNameIndex,RecordNameLength);
                  //cerr << "Checking '" << recordName << "'\n";

                  // Look for other records in the round robin with the same name
                  for (int index = 0;index < 7;index++) {

                        // Here's the line to check against
                        const string& check_line = round_robin[index];

                        // Don't bother checking against same index and only check againsts lines long enough
                        if (index != working_index && check_line.size() > OperatedIndex)

                              // Look for Operated
                              if (check_line.substr(OperatedIndex,OperatedLength) == "Operated") {

                                    // Read off the record name
                                    const string& check_recordName = check_line.substr(RecordNameIndex,RecordNameLength);

                                    //cerr << "Checking against '" << check_recordName << "'\n";

                                    // If the record names match, log it
                                    if (recordName == check_recordName)
                                          os << recordName << '\n';
                              }
                  }
            }
            //else {
            //      cerr << "Index(" << working_index << "): " << working_line.substr(OpenIndex,OpenLength) << '\n';
            //}
      }
}
--------8<--------
0
 

Author Comment

by:ccjong
Comment Utility
Thank you very much for your help and advice.

I tried to compile the code and encountered the following error message.

checkopen.cpp: In function `int main(int, const char**)':
checkopen.cpp:25: error: ISO C++ forbids declaration of `input_index' with no  type
checkopen.cpp:31: error: ISO C++ forbids declaration of `working_index' with no type
checkopen.cpp:44: error: ISO C++ forbids declaration of `input_index' with no  type
checkopen.cpp:50: error: ISO C++ forbids declaration of `working_index' with no  type

Need to define the type?

My OS is fedora core 1 and the c++ compiler - g++ 3.2.3

Thanks again...

0
 

Author Comment

by:ccjong
Comment Utility
I use int for both input_index and working_index. Compiled now without error. Thanks.
0
 

Author Comment

by:ccjong
Comment Utility
The code is working perfectly.

Thanks you very much...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now