• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • 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!!


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

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.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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;

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.

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.

#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

            // Adjust 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

            // 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


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';
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...

ccjongAuthor Commented:
I use int for both input_index and working_index. Compiled now without error. Thanks.
ccjongAuthor Commented:
The code is working perfectly.

Thanks you very much...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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