Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I Suck

Name: Anonymous 2012-07-22 11:49

I'm taking an intro C++ class, the only thing I have experience with is Java (only thing offered and my self-teaching has been with irrelevant things such as lisp) and have a quick question.

I'm trying to break the lines of a text file into separate strings to be input into a struct, using a colon as the token. I have no idea how to go about this in C++ though, can someone shed some light here?

I don't need nor want for someone to write my program for me, I'm just not sure how to segregate the lines into distinct strings.

Name: Anonymous 2012-07-22 11:55

An example line looking something like:

Sleepyhead        :    Passion Pit     :Manner

In this scenario, I'm trying to get three strings: "Sleepyhead", "Passion Pit", "Manner".

Name: Anonymous 2012-07-22 14:17

Use strtok to split the string at ":", then just trim the whitespace.

Name: Anonymous 2012-07-22 18:21


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>

#define QWORD unsigned long long int
#define DWORD unsigned int

namespace text
{
  typedef unsigned int status;
  static const status word_size_exceeded = 0x01;
  static const status out_of_dest_bounds = 0x02;

  template<size_t x, size_t y>
  QWORD split_at_colon(
    char dest[x][y],
    char src[]
    )
  {
    static const int max_word_size = 100;
    int internal_x = 0,
      internal_y = 0;

    char buffer[max_word_size];
    QWORD ret = 0;

    for(char * psrc = src, * pbuf = buffer;
      *psrc != 0; ++psrc)
    {
      if(*(psrc + 1) == 0)
      {
        *(pbuf++) = *psrc;
        goto handle_colon;
      }

      switch(*psrc)
      {
      case ' ':
          continue;

      case ':':
        {
handle_colon:
          *pbuf = 0;
         
          //bounds check first
          internal_y = pbuf - buffer + 1;
          if(internal_x > x || internal_y > y)
            return *((DWORD *) &ret) = out_of_dest_bounds;
         
          //write buffer to destination
          memcpy(dest[internal_x++],
            buffer, internal_y);
          //words read:
          ++*((DWORD *) &ret + 1);

          /*
          reset buffer ptr; we're ready
          to read a new word into buffer
          */
          pbuf = buffer;
         
          continue;
        }
      }

      if(pbuf - buffer < max_word_size)
        *(pbuf++) = *psrc;
      else
        return *((DWORD *) &ret) = word_size_exceeded;
    }

    return ret;
  }
}

struct your_mother
{
  char * i;
 
  char * f;
  char * u;
  char * c;
  char * k;
  char * e;
  char * d;

  char * h;
  char * e_;
  char * r;
};
 
int main(void)
{
  char dest[50][50];
  char src[] = "lisp : is : shit"; 
  QWORD err =
    text::split_at_colon<50, 50>(dest, src);
  assert((DWORD) err ^ text::out_of_dest_bounds);
  assert((DWORD) err ^ text::word_size_exceeded);
  int words_read = *((DWORD *) &err + 1);

  /* Demo: print all the words so far */
  for(int i = 0; i != words_read; ++i)
  {
    printf("%s\n", dest[i]);
  }

  /* Demo: fill struct */
  your_mother lel;
  lel.i = dest[0];
  lel.f = dest[1];
  lel.u = dest[2];

  return 0;
}


probably not enterprise-quality BOOST enough though

Name: Anonymous 2012-07-22 18:23

>>3
strtok is not thread safe

Name: Anonymous 2012-07-22 18:29

>>5
There's strtok_r in GNU C if you need to split strings at same time in several threads.
Which you don't in most circumstances, and if you really do, can't use GNU C and aren't willing to use/write alternative functions, can always get around with mutexes.

Name: Anonymous 2012-07-22 18:31

>>6
GNU
i vomited a little

Name: Anonymous 2012-07-22 18:34

>>7
Enjoy your Microsoft Visual C++!

Name: Anonymous 2012-07-23 10:51

lel

Name: Anonymous 2012-07-23 11:01

>>6
can always get around with mutexes.
You'll have to lock out all the other threads for the entire duration of tokenization, it's just not feasible.

Name: Anonymous 2012-07-23 14:15

>>10
Yes, you would have to do that, but that shouldn't be very long and it would be unnoticable (unless that's the main function of your program and it does it all the time on huge amounts of data, in which case go write your own version of that function).

Name: Anonymous 2012-07-23 21:37

>>8

HAHA! Yes, everything's nice and dandy with Micro$oft IDEs!

Name: Anonymous 2012-07-23 21:45

>>8

I will, It's incredible.

Stay mad freetard.

Name: Anonymous 2012-07-23 21:51

>>11
Which scenario do you expect you would have to use strtok from multiple threads yet not needing performance? If you look out every other thread for the duration of tokenization then you basically have a single threaded program.

Name: Anonymous 2012-07-23 23:50

` >being a professional codemonkey
 >2012

LOL!
Signed,
A Professional Engineer

Name: Anonymous 2012-07-23 23:59

>>13
Stay retard, fucktard.

Name: Anonymous 2012-07-24 0:11

>>15
>engineering
>software
Heh.jpg . I bet that you construct food also right?

Name: Anonymous 2012-07-24 0:14

>>17
Everyone knows Software Engineering isn't real engineering, dimwit.

Name: Anonymous 2012-07-24 0:29

>>13
tell your boss that it's a waste of time to viral prauge

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-07-24 5:10


while(ptr=strchr(ptr, ':')) {
 /* left as an exercise for the reader */
}

Name: Anonymous 2012-07-24 13:32

>>14
I can't really imagine a realistic scenario where you'd need to use strtok in multiple threads at once for performance. If you're using it in several threads, then it's likely something like an event-driven program whose main purpose isn't string tokenization.

Don't change these.
Name: Email:
Entire Thread Thread List