Extracting data from a file in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lana rose
    New Member
    • Nov 2006
    • 8

    Extracting data from a file in C++

    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.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void main ()
    {
                    string find_this_string = "begin MEASUREMENT";
    	
    	fstream datain, dataout;
    	string current_line_in_file;
    	
    	cout << "Reading infomation from an A2L file \n";
    	
    	datain.open("examplea2l.txt",fstream::in);
    	dataout.open("librarya.txt",fstream::out);
    
    	while (getline(datain, current_line_in_file, '\n'))
    	{
                        string::size_type pos = current_line_in_file.find(find_this_string,0);
    
                        if (pos != string::npos) 
    		{
    	        
    			dataout << current_line_in_file << "\n";
    		}
    	    else
    		{
    			/* do nothing */
    
    		}
    	}
    	
    
    	datain.close();
    	dataout.close();
    
    }
    I hope that I have explained my problem enough for you to help me.

    Thank you

    Lana
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      have a look at this
      Code:
      #include <iostream>
      #include <fstream>
      #include <string>
      using namespace std;
      
      int main ()
      {
          string find_this_string = "begin MEASUREMENT";
      	string outputFile="librarya.txt";
      	char ch='a';
      	fstream datain, dataout;
      	string current_line_in_file;
      	
      	cout << "Reading infomation from an A2L file \n";
      	
      	datain.open("examplea2l.txt",fstream::in);
         // look for begin of data
      	while (getline(datain, current_line_in_file, '\n'))
      	{ 
           string::size_type pos = current_line_in_file.find(find_this_string,0);
           if (pos != string::npos) 
      		{
               outputFile[7]=ch++;                            // set filename a, b, c, etc
        	     dataout.open(outputFile.c_str(),fstream::out); // open output file
      	     dataout << current_line_in_file << "\n";       // write first line
      	     while (getline(datain, current_line_in_file, '\n'))
         	      { 
      	       dataout << current_line_in_file << "\n";      // write next line
                 string::size_type pos = current_line_in_file.find("/end MEASUREMENT ",0);
                 if (pos != string::npos) break;               // if end exit while() loop
                 }
               dataout.close();
        	    }
      	}
      	datain.close();
      }
      the first output file is called librarya.txt, the next libraryb.txt, the next libraryc.txt, etc

      Comment

      Working...