• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Need Help for the C++ program

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
ccjong
Asked:
ccjong
  • 5
  • 3
1 Solution
 
rstaveleyCommented:
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
 
ccjongAuthor Commented:
Really appreciate if you can show me with the C++ codes.

Thanks.
0
 
rstaveleyCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ccjongAuthor Commented:
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
 
rstaveleyCommented:
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
 
ccjongAuthor Commented:
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
 
ccjongAuthor Commented:
I use int for both input_index and working_index. Compiled now without error. Thanks.
0
 
ccjongAuthor Commented:
The code is working perfectly.

Thanks you very much...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now