By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

parse tilde delimited file

P: n/a
Hello, I need to parse a tilde delimited file and output it to a
tabbed delimited file. Example file example.txt

data1~data2~data3~data4
data5~data6~data7~data8

I need to extract data2, data4, data6 and data8 from the above file
and output it to a file delimited by tabs:

data2 data4 data6 data8

I know how to do the ouput but the parsing I'm not sure about. Thanks
for your help. Monte.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"monte" <ol******@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Hello, I need to parse a tilde delimited file and output it to a
tabbed delimited file. Example file example.txt

data1~data2~data3~data4
data5~data6~data7~data8

I need to extract data2, data4, data6 and data8 from the above file
and output it to a file delimited by tabs:

data2 data4 data6 data8

I know how to do the ouput but the parsing I'm not sure about. Thanks
for your help. Monte.


Actually you have two sets of delimiters, end of line and ~. So you need two
nested loops, the outer reading lines and the inner reading ~ delimited
text.

How about this (untested)

std::string line;
while (getline(file, line))
{
std::istringstream buffer(line);
std::string data;
while (getine(buffer, data, '~'))
{
// do something with data
}
}

john
Jul 22 '05 #2

P: n/a
> Actually you have two sets of delimiters, end of line and ~. So you
need two
nested loops, the outer reading lines and the inner reading ~ delimited text.


If all what needs to be done is replacing the tilde characters by tab
characters, I would just do that:

| #include <fstream>
| #include <algorithm>
| #include <iterator>
| int main() {
| std::ifstream in("in.file");
| std::ofstream out("out.file");
| std::replace_copy(std::istreambuf_iterator<char>(i n),
| std::istreambuf_iterator<char>(),
| std::ostreambuf_iterator<char>(out),
| '~', '\t');
| }

If speed does matter, you will need a good standard C++ library
implementation for this or you might indeed want to read and
process individual lines. However, 'std::getline()'ing a line and
'std::replace()'ing the tilde characters should do the trick.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3

P: n/a
monte wrote:
Hello, I need to parse a tilde delimited file and output it to a
tabbed delimited file. Example file example.txt


Oh, that's right - it's September. How could I have forgotten?

--
Mike Smith
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.