Solved

Need Help for the C++ program

Posted on 2004-10-05
8
215 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
[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
  • 5
  • 3
8 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12235564
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
ID: 12235721
Really appreciate if you can show me with the C++ codes.

Thanks.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12235780
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ccjong
ID: 12236052
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
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 12236172
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
ID: 12236448
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
ID: 12236481
I use int for both input_index and working_index. Compiled now without error. Thanks.
0
 

Author Comment

by:ccjong
ID: 12236513
The code is working perfectly.

Thanks you very much...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

729 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