By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Extracting data from a file in C++

P: 8
I am trying to produce some code in C++ that will be able to scan through a mixed document and extract specific lines of data. The document will look like this (below) but will have hundreds of these 'begin measurement' and 'end measurement' markers with different text within each one.

/begin MEASUREMENT
E2_PCU_90V "The 90v rail PWM trim value"
UWORD
CM_136
1 0
0 65535
ECU_ADDRESS 0x0006B5C6
/end MEASUREMENT

/begin MEASUREMENT
test_Array_Fill_APV "Temporary unsigned "
ULONG
CM_134
1 0
0 4294967295
ECU_ADDRESS 0x0006B5C8
/end MEASUREMENT

I have written a bit of code that will scan through the document and find every 'begin measurement' and print it in a different file. I am now stuck as I need it to extract the lines in-between the 'begin measurement' and 'end measurement' and print them in the other file.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void main ()
  8. {
  9.                 string find_this_string = "begin MEASUREMENT";
  10.  
  11.     fstream datain, dataout;
  12.     string current_line_in_file;
  13.  
  14.     cout << "Reading infomation from an A2L file \n";
  15.  
  16.     datain.open("examplea2l.txt",fstream::in);
  17.     dataout.open("librarya.txt",fstream::out);
  18.  
  19.     while (getline(datain, current_line_in_file, '\n'))
  20.     {
  21.                     string::size_type pos = current_line_in_file.find(find_this_string,0);
  22.  
  23.                     if (pos != string::npos) 
  24.         {
  25.  
  26.             dataout << current_line_in_file << "\n";
  27.         }
  28.         else
  29.         {
  30.             /* do nothing */
  31.  
  32.         }
  33.     }
  34.  
  35.  
  36.     datain.close();
  37.     dataout.close();
  38.  
  39. }
  40.  
I hope that I have explained my problem enough for you to help me.

Thank you

Lana
Nov 15 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
within your existing while() loop you have a similar loop which reads lines from datain writing them to dataout until you find the "/end MEASUREMENT " line
Nov 15 '06 #2

Expert 100+
P: 1,510
have a look at this
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8.     string find_this_string = "begin MEASUREMENT";
  9.     string outputFile="librarya.txt";
  10.     char ch='a';
  11.     fstream datain, dataout;
  12.     string current_line_in_file;
  13.  
  14.     cout << "Reading infomation from an A2L file \n";
  15.  
  16.     datain.open("examplea2l.txt",fstream::in);
  17.    // look for begin of data
  18.     while (getline(datain, current_line_in_file, '\n'))
  19.     { 
  20.      string::size_type pos = current_line_in_file.find(find_this_string,0);
  21.      if (pos != string::npos) 
  22.         {
  23.          outputFile[7]=ch++;                            // set filename a, b, c, etc
  24.            dataout.open(outputFile.c_str(),fstream::out); // open output file
  25.          dataout << current_line_in_file << "\n";       // write first line
  26.          while (getline(datain, current_line_in_file, '\n'))
  27.              { 
  28.            dataout << current_line_in_file << "\n";      // write next line
  29.            string::size_type pos = current_line_in_file.find("/end MEASUREMENT ",0);
  30.            if (pos != string::npos) break;               // if end exit while() loop
  31.            }
  32.          dataout.close();
  33.           }
  34.     }
  35.     datain.close();
  36. }
  37.  
the first output file is called librarya.txt, the next libraryb.txt, the next libraryc.txt, etc
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.