parse tilde delimited file

M

monte

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.
 
J

John Harrison

monte said:
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
 
D

Dietmar Kuehl

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::eek:fstream out("out.file");
| std::replace_copy(std::istreambuf_iterator<char>(in),
| std::istreambuf_iterator<char>(),
| std::eek:streambuf_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.
 
M

Mike Smith

monte said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top