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
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.
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
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) );
}
}
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
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.
Re: Split into records in CPP
Quote:
Originally Posted by
Lindley
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'.
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.
Re: Split into records in CPP
strtok can be used to split strings, if that is what you want o_O.