473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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.

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
2 7423
horace1
1,510 Expert 1GB
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
horace1
1,510 Expert 1GB
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

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

Similar topics

2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
0
by: Nadav | last post by:
Hi, Introduction: *************************** I am using the MSI API to extract MSI embedded files, I do this by iterating through all of the records in the ‘_Streams’ table and dumping...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
0
by: Sunil Basu | last post by:
Hi, I have a interesting thing to know and discuss with you. I am extracting data from an Excel file in a Delphi DbGrid through SQL. I want to create a criteria on a specific cell value of the...
0
by: sgsiaokia | last post by:
I need help in extracting data from another source file using VBA. I have problems copying the extracted data and format into the required data format. And also, how do i delete the row that is not...
6
by: Amma | last post by:
Hello Every one , Pls help me to extracting number from a text file since I am new to perl programming . I have a file and need to extract the number after semicolon in that ...
6
by: rlntemp-gng | last post by:
I need to extract information from some Excel files but am stuck with part of it: As an example, I have the following Excel File that has 3 tabbed sheets: FileName: ...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.