View Full Version : Data extract
jtrette
06-03-2008, 09:44 PM
I am new to C and I am looking to find a way to extract certain elements of a text file. The file has a whole bunch of data in it but I only need certain parts so I am trying to figure out how to use markers that are consistant through the file and take the data immediatly following. Basically make like a ticker tape and have the program spit out the data that I need. Does anyone know where I could find more information about this? Thanks for the help.
tomws
06-03-2008, 11:12 PM
I think you might be interested in tokenizing. Check out the function strtok().
jtrette
06-04-2008, 06:20 PM
Ok so I tried to get some code working to do my extracting and I have it working but it doesn't find values. I am a novice so I don't know much, the code runs because it asks me the questions and responds but it does not find values that I know are there. I attached the file if anyone will take a look and tell me maybe something I did wrong. What I am trying to tell it to do is to find a tag and then take the numbers between the 2 asterisks following. For example if the file has ~CLP*1234* and I tell it the tag line is ~CLP I should get 1234 back. 6406
tomws
06-04-2008, 08:31 PM
Paste your code into code tags here. That text file isn't plain text. Anyway, more people may offer advice if your code is readily available here.
jtrette
06-04-2008, 09:36 PM
Here is the code that I wrote. This is referring to the comment that I left 2 comments above. Let me know if anyone has any ideas.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int delimFcn(string, string, string);
int main()
{
cout << "********************" << endl;
cout << "REMIT DELIMITOR\n" << << endl;
cout << "********************" << endl << endl;
ifstream infile;
string fileName;
string buffer;
string delim;
string output;
cout << "File Name? ";
cin >> fileName;
cout << endl;
cout << "Newline Tag? ";
cin >> delim;
cout << endl;
cout << "Output file name? ";
cin >> output;
cout << endl;
infile.open(fileName.c_str(), ios::in);
string temp;
while(1)
{
temp.clear();
infile >> temp;
if(temp.empty())
{
break;
}
buffer.append(temp);
}
int success = delimFcn(buffer, delim, output);
if(success == 0)
{
cout << "NO VALUES FOUND!" << endl;
}
else
{
cout << "COMPLETE: " << success << " values found!" << endl;
}
infile.close();
return 0;
}
int delimFcn(string buffer, string delim, string output)
{
int success = 0;
int bufLev = 0;
int hit = delim.length();
int astr = 0;
bool extract = false;
ofstream outfile;
outfile.open(output.c_str(), ios::out);
cout << "Parsing with ";
for(int i = 0; i < delim.length(); i++)
{
cout << delim[i];
}
cout << endl;
for(int i = 0; i < buffer.length(); i++)
{
if(buffer[i] == delim[0])
{
bufLev++;
for(int j = 1; j < hit; j++)
{
if(buffer[i+j] == delim[j])
{
bufLev++;
}
}
if(bufLev == hit)
{
extract == true;
bufLev = 0;
}
else
{
cout << bufLev << endl;
bufLev = 0;
}
}
if(extract)
{
if(buffer[i] == '*')
{
astr++;
}
if(astr == 2)
{
outfile << "\n";
bufLev = 0;
extract = false;
astr = 0;
success++;
}
if(astr == 1 && buffer[i] != '*')
{
outfile << buffer[i];
}
}
}
outfile.close();
return success;
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.