Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Data extract

  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Data extract

    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.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I think you might be interested in tokenizing. Check out the function strtok().

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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. delim.txt

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:ut);

    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;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •