Re: [C++] Help Parsing an Integer/Character Mix

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 02/21/04


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



Relevant Pages

  • [PATCH] Numerous fixes to kernel-doc info in source files.
    ... static inline int ffs ... @buffer: where the data must be copied. ... * struct kfifo with kfree. ... @timer: ...
    (Linux-Kernel)
  • [UNIX] Multiple Vulnerabilities in Citadel/UX
    ... could allow complete control over a vulnerable server. ... Citadel server as can be seen by this simplistic code snippet: ... configuration buffers, leading to the possibility of carrying out a buffer ... int connect_to_host; ...
    (Securiteam)
  • Re: pushing the envelope with sockets
    ... receiving on the socket they are received (upto the buffer size), you can even change what happens if the buffer runs full. ... int read = S.EndReceive; ... class AsyncReader: Reader ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Updated: Reworked Cell OProfile: SPU mutex lock fix
    ... arch-independent pieces of the OProfile kernel driver that this patch ... kernel driver patches. ... into the kernel buffer without holding the buffer_mutex lock. ... int spu_sync_start; ...
    (Linux-Kernel)
  • [PATCH] char: make functions static in synclinkmp.c
    ... int *eof, void *data) ... /* Allocate DMA buffers for the transmit and receive descriptor lists. ... * This buffer is used to pass an assembled frame to the line discipline. ...
    (Linux-Kernel)