Re: [C++] Help Parsing an Integer/Character Mix
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 02/21/04
- Next message: osmium: "Re: Determining probability for a variable number of results?"
- Previous message: April: "Determining probability for a variable number of results?"
- In reply to: entropy123: "[C++] Help Parsing an Integer/Character Mix"
- Next in thread: entropy123: "Re: [C++] Help Parsing an Integer/Character Mix"
- Reply: entropy123: "Re: [C++] Help Parsing an Integer/Character Mix"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 22 Feb 2004 01:11:23 +1100
"entropy123" <email_entropy123@yahoo.com> wrote in message
news:90cdce37.0402201257.700aa672@posting.google.com...
| This question goes to the heart of my troubles with C++, input gives
| me fits. I've scoured the nets and several books to no avail. Any
| strategies, advice much appreciated!
|
| I have this text file.
|
| It reads:
|
| 2 A0032 B0054 3 B3203
|
| What I need to do is fill my datastructure like this:
|
| 2 A0032
| 1 B0054
| 3 B3203
[snip]
| PS: I would also like to parse 'A0032' into 'A' and '32', 'B3203' into
| 'B' and '3203'.....advice here mucho appreciated...
[snip]
All of the above is not really a very difficult task to accomplish,
but you must have some idea as to how to approach it before you
begin. Additionally, when trying to explain your problem, please
make it as clear as practically possible.
For example:
- Does the file format(tokens) change at all ?
- What kind of data structure are you using ?
- What are the crucial parts of reading this file,
and what is an error ?
- etc...
In any case, here is an example to help do what you require,
at least as far as I can gather - hope it helps.
- Note: I have left out error checking to keep it short(er):
# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <vector>
# include <cstdlib>
# include <utility>
struct Data
{
std::string::size_type Idx;
std::pair<int, std::string> MyPair;
std::pair<char, std::string> NewPair;
Data( int num, std::string token )
: Idx( 0 ), MyPair( std::make_pair( num, token ) )
{
if( ( Idx = token.find( "00" ) ) != std::string::npos )
NewPair = std::make_pair( token[ 0 ], token.substr( Idx + 2 ) );
else
NewPair = std::make_pair( token[ 0 ], token.substr( 1 ) );
}
};
int main()
{
char* EndPtr( 0 );
int Number( 0 );
std::string Code;
std::string Buffer;
std::vector<Data> V;
std::ifstream InFile( "Entropy.txt" );
while( InFile >> Buffer )
{
Number = std::strtol( Buffer.c_str(), &EndPtr, 10 );
if( EndPtr == ( Buffer.c_str() + Buffer.size() ) )
{
InFile >> Buffer;
V.push_back( Data( Number, Buffer ) );
}
else
V.push_back( Data( 1, Buffer ) );
}
std::vector<Data>::const_iterator Idx( V.begin() );
for( Idx; Idx != V.end(); ++Idx )
std::cout << Idx -> MyPair.first << " "
<< Idx -> MyPair.second << "\t\t"
<< Idx -> NewPair.first << " "
<< Idx -> NewPair.second << std::endl;
return 0;
}
-- Entropy.txt --
2 A0032 B0054 3 B3203
C0038 6 E0059 8 F3201
5 A0045 7 B3260 8 B3111
2 D3345 4 A0060 E3111
-- Output --
2 A0032 A 32
1 B0054 B 54
3 B3203 B 3203
1 C0038 C 38
6 E0059 E 59
8 F3201 F 3201
5 A0045 A 45
7 B3260 B 3260
8 B3111 B 3111
2 D3345 D 3345
4 A0060 A 60
1 E3111 E 3111
Ideally, you could wrap all the above code in 'main()', into
an function of your choice, possible even an member function.
If you have any questions about the code,
then please feel free to ask.
Cheers.
Chris Val
- Next message: osmium: "Re: Determining probability for a variable number of results?"
- Previous message: April: "Determining probability for a variable number of results?"
- In reply to: entropy123: "[C++] Help Parsing an Integer/Character Mix"
- Next in thread: entropy123: "Re: [C++] Help Parsing an Integer/Character Mix"
- Reply: entropy123: "Re: [C++] Help Parsing an Integer/Character Mix"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|