Split into records in CPP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Split into records in CPP

  1. #1
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    962

    Split into records in CPP

    Hi C++ gurus;
    Code:
                string strBinAll = s0.ReadToEnd();
                s0.Close();
                string[] records = Regex.Split(strBinAll, "\r\n");
    in C# .net one can easily split a chunk of data by e record delimiter into an array...

    Now in Vc++ 6.0 is there anything that can get the job done without writing something yourself ?

    Cheers

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: Split into records in CPP

    Just typed in ... might be typos ...

    Code:
    stringstream ss(strBinAll);
    vector<string> records;
    
    string rec;
    
    while (getline(ss,rec))
    {
       records.push_back(rec);
    }
    Did you read from a file ? You could have just read them into
    records to start with.

  3. #3
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    962

    Re: Split into records in CPP

    Code:
    istream& getline (char* s, streamsize n );
    istream& getline (char* s, streamsize n, char delim );
    Hi
    Thanks for the reply.
    The file is in binary .... records may contain control characters, however records are terminated by \r\n\r\n.
    Therefore reading the file as a text file and format wont work really.
    I need to be able to split them into records that terminate by a record terminator \r\n\r\n.

    I m not sure if getline woudl be able to offer me what I need.

    Cheers

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: Split into records in CPP

    getline() will only work with a single character.

    It is very unusual for binary files to have \r\n ... that is
    usually only a Windows text fille thing.

    Can there be \n without a \r before it ? If not, then
    you should be able to use the code I posted ... you would need
    to remove the last character (which would be a \r).


    Otherwise, you will need to write it yourself. std:string::find and substr().

    in your first post, you just had a single \r\n ... in your last post a
    double \r\n\r\n ... which is it ?

    Also, I believe if a file is opened in text mode, \r\n is translated to \n
    on input.

    Something like this should work (not tested):

    Code:
    void SplitString(const string & s , vector<string> & v , const string & delim)
    {
        const string::size_type delim_length = delim.size();
    
        string::size_type start = 0;
    
        string::size_type pos = s.find(delim);
    
        while (pos != string::npos)
        {
            v.push_back( s.substr(start,pos-start) );
    
            start = pos + delim_length;
    
            pos = s.find(delim,start);
        }
    
        if (start != s.size())
        {
           v.push_back( s.substr(start) );
        }
    }
    Last edited by Philip Nicoletti; March 25th, 2010 at 11:08 PM.

  5. #5
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    962

    Re: Split into records in CPP

    Thanks for teh reply...
    What you have posted shoudl deal with a string terminator ,\r\n or \r\n\r\n
    An issue has risen and I know it has to do with having some binary in the record .from exp i know the rec terminator is either of those (assuming the binary bit in the record will NEVER have \r\n or \r\n\r\n in it..

    Anyway thanks for your reply and i ll give this a go once I examine the input file throughly on Monday.
    Your posts have been helpful

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Split into records in CPP

    On a Windows system, a file opened in text mode will translate every \r\n pair to just \n. Therefore, getline() will work properly on it. You'll simply have to check whether getline() results in an empty string in order to identify \r\n\r\n sequences.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,553

    Re: Split into records in CPP

    Quote Originally Posted by Lindley View Post
    On a Windows system, a file opened in text mode will translate every \r\n pair to just \n. Therefore, getline() will work properly on it. You'll simply have to check whether getline() results in an empty string in order to identify \r\n\r\n sequences.
    This will corrupt records that contain '\r\n'.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Split into records in CPP

    True. Whether that matters or not is an important question, though. Typically, I'd expect records separated by a newline to be textual rather than binary images or something, in which case it probably doesn't matter.

  9. #9
    Join Date
    Mar 2010
    Location
    Aalten, Holland
    Posts
    14

    Re: Split into records in CPP

    strtok can be used to split strings, if that is what you want o_O.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center