Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. char buf

char buf

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
16 Posts 11 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Smith

    How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

    S Offline
    S Offline
    sunit5
    wrote on last edited by
    #2

    How about Link list typedef struct list { char c; list *p; }; never say die

    1 Reply Last reply
    0
    • S Smith

      How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #3

      Use a std::vector of chars #include <vector> typedef std::vector<char> TCharArray; TCharArray myChars; int anysizeyoulike = 42; myChars.resize( anysizeyoulike ); you can then work with myChars as if it were a regular array myChars[10] = 'a'; (just remember to resize it again if you want it to be a different size, as bad things will happen if you go off the end of your buffer!) -- Help me! I'm turning into a grapefruit! Buzzwords! -- modified at 9:24 Wednesday 11th January, 2006

      S E 2 Replies Last reply
      0
      • B benjymous

        Use a std::vector of chars #include <vector> typedef std::vector<char> TCharArray; TCharArray myChars; int anysizeyoulike = 42; myChars.resize( anysizeyoulike ); you can then work with myChars as if it were a regular array myChars[10] = 'a'; (just remember to resize it again if you want it to be a different size, as bad things will happen if you go off the end of your buffer!) -- Help me! I'm turning into a grapefruit! Buzzwords! -- modified at 9:24 Wednesday 11th January, 2006

        S Offline
        S Offline
        Smith
        wrote on last edited by
        #4

        Nice, but i want the size to be based on the number of characters the user enters. how to get it? myChars.resize( anysizeyoulike );// anysizeyoulike should be differing based on the No. of char. the user is entering. getchar() will be of any use? regards, Rookie Installing MFC...2% complete

        B 1 Reply Last reply
        0
        • B benjymous

          Use a std::vector of chars #include <vector> typedef std::vector<char> TCharArray; TCharArray myChars; int anysizeyoulike = 42; myChars.resize( anysizeyoulike ); you can then work with myChars as if it were a regular array myChars[10] = 'a'; (just remember to resize it again if you want it to be a different size, as bad things will happen if you go off the end of your buffer!) -- Help me! I'm turning into a grapefruit! Buzzwords! -- modified at 9:24 Wednesday 11th January, 2006

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #5

          if you need to tell the "anysize", why not this work? do { //enter anysize; anysize =4; //user entered char *str= new char(anysize); }while


          0x0400: "But your mind is very complex, very tricky. It makes simple things complicated. -- that's its work. And for centuries it has been trained for only one thing: to make things so complicated that your life becomes impossible."- Osho

          --[V]--

          1 Reply Last reply
          0
          • S Smith

            How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #6

            That's where you should use a string wrapper like the MFC/ATL CString or C++ std::string. Regards, Nish

            My blog : Nish’s thoughts on MFC, C++/CLI and .NET

            1 Reply Last reply
            0
            • S Smith

              How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

              V Offline
              V Offline
              vipinasda
              wrote on last edited by
              #7

              Use string or MFC CString. Using them would help you in all of this:- 1) Don't have to bother about freeing memory after usage. 2) Potential crashes by working directly with pointers and possible buffer overflows. Both of them become issues when you write a program which would grow into several 1000 lines of code and ultimately end up in ugly event of crashes on customer machines. As part of coding style, make them part of your company or personal coding practice and follow them from the day one you start working on a project. If not followed you would endup in unmaintainable code, with memory allocations at one place and deallocation a mile away.memory leaks and troubles which could be avoided at first place. That's were c++ comes to your rescue, use them to the fullest wherever you can. Vipin - MVP

              1 Reply Last reply
              0
              • S Smith

                How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

                L Offline
                L Offline
                lemur2
                wrote on last edited by
                #8

                You're asking one of two things: Q1) how do I create an adjustable-length string to read data into? A - use something like std::vector instead Q2) how do I read in an arbitrary length of user data and minimise the amount of temporary storage required? A - basically, if you don't know in advance how much data there will be, you can only make an educated guess, then shorten the string to the correct size at the end. To keep things to an *absolute minimum*, you read a character at a time and resize and copy the string each new character. However, this is theoretical - it would be dog slow and would probably fragment memory hideously for long message. A common solution is to start off with a buffer (use a std::vector again) and read as many bytes in a chunk from your socket as it would take to fill this. If you've finished reading, stop and shorten your vector. If there's more text, increase the length of the string and read more. You could increase by the same amount (so your string lengths are N, 2N, 3N etc) or double it each time (giving lengths of N, 2N, 4N etc), or any other method. What you want to do is minimise the amount of memory copying and number of socket reads. Hope that helps (homework? :-D ) Kev

                S 1 Reply Last reply
                0
                • S Smith

                  How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

                  S Offline
                  S Offline
                  Sebastian Schneider
                  wrote on last edited by
                  #9

                  hmm? char inChar[2] = {0,0}; char* pTemp = 0; char* pText = 0; int length = 2; inChar[0] = fgetc(stdin); pText = (char*)malloc(sizeof(char)*length); strcpy(pText,&inChar[0]); inChar[0] = fgetc(stdin); while((int)inChar[0] != 10) { length++; pTemp = pText; pText = (char*)malloc(sizeof(char)*length); strcpy(pText,pTemp); pText[length-2] = inChar[0]; pText[length-1] = 0; free(pTemp); inChar[0] = fgetc(stdin); } free(pText); Its optimizable. But I didnt have time to optimize the first step out ;) Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                  1 Reply Last reply
                  0
                  • L lemur2

                    You're asking one of two things: Q1) how do I create an adjustable-length string to read data into? A - use something like std::vector instead Q2) how do I read in an arbitrary length of user data and minimise the amount of temporary storage required? A - basically, if you don't know in advance how much data there will be, you can only make an educated guess, then shorten the string to the correct size at the end. To keep things to an *absolute minimum*, you read a character at a time and resize and copy the string each new character. However, this is theoretical - it would be dog slow and would probably fragment memory hideously for long message. A common solution is to start off with a buffer (use a std::vector again) and read as many bytes in a chunk from your socket as it would take to fill this. If you've finished reading, stop and shorten your vector. If there's more text, increase the length of the string and read more. You could increase by the same amount (so your string lengths are N, 2N, 3N etc) or double it each time (giving lengths of N, 2N, 4N etc), or any other method. What you want to do is minimise the amount of memory copying and number of socket reads. Hope that helps (homework? :-D ) Kev

                    S Offline
                    S Offline
                    Sebastian Schneider
                    wrote on last edited by
                    #10

                    I totally agree. Nevertheless, I wrote EXACTLY what he wanted (and I hope it works, I didnt test it) ;) I love buffered streams... Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                    1 Reply Last reply
                    0
                    • S Smith

                      How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

                      E Offline
                      E Offline
                      Eytukan
                      wrote on last edited by
                      #11

                      not fully structured.. i typed just like that...

                      typedef struct node
                      {
                      char chr;
                      int index;
                      struct node* nChr;
                      }chatSet_t;

                      char *makeString(struct node* Start,int size)
                      {
                      int j=0;
                      struct node* newStr =Start;

                      char *myString =new char(size);

                      while(jchr;
                      newStr=newStr->nChr;
                      j++;
                      }
                      myString[size]='\0';
                      return(&myString[0]);
                      }

                      int main(int argc, char* argv[])
                      {

                      char *actualString;
                      struct node* head = new (node);
                      struct node* newString = new (node);

                      int i=0;
                      head=newString;
                      char x;
                      do
                      {
                      x = getchar();
                      newString->chr =x;
                      newString->index =i;
                      newString->nChr =new(node);
                      newString=newString->nChr;
                      i++;
                      }while(x!=10);

                      actualString = makeString(head,i);
                      printf("\n%s",actualString);

                      getch();

                      return 0;
                      

                      }

                      i did it.. but just not error proof :)


                      0x0400: "But your mind is very complex, very tricky. It makes simple things complicated. -- that's its work. And for centuries it has been trained for only one thing: to make things so complicated that your life becomes impossible."- Osho

                      --[V]--

                      1 Reply Last reply
                      0
                      • S Smith

                        Nice, but i want the size to be based on the number of characters the user enters. how to get it? myChars.resize( anysizeyoulike );// anysizeyoulike should be differing based on the No. of char. the user is entering. getchar() will be of any use? regards, Rookie Installing MFC...2% complete

                        B Offline
                        B Offline
                        benjymous
                        wrote on last edited by
                        #12

                        If you're getting characters from the keyboard then try something like: #include typedef std::vector TCharArray; TCharArray myChars; while( char ch=getchar() ) { myChars.push_bach(ch); } myChars.size() will tell you how many chars you've got -- Help me! I'm turning into a grapefruit! Buzzwords!

                        1 Reply Last reply
                        0
                        • S Smith

                          How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

                          B Offline
                          B Offline
                          Bob Stanneveld
                          wrote on last edited by
                          #13

                          Hello, I don't know why nobody give the answer: std::istringstream. This class is designed to build strings of unknown length by appending segments to the buffer (stream) just like you would use std::cout. The code would look like:

                          // Use a character buffer for reading characters from the socket.
                          // Make sure that a '\0' follows the character sequence, otherwise the behaviour
                          // of the istringstream class is undefined.
                          std::istringstream istrInputString;
                          char cBuf[100] = {0};
                          while( /* there are bytes from your socket */
                          {
                          /* read bytes from socket and appen terminating null character. */
                          istrInputString << cBuf;
                          }

                          // Do something with the string.

                          See MSDN[^] for more details. Hope this helps. :) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                          T 1 Reply Last reply
                          0
                          • B Bob Stanneveld

                            Hello, I don't know why nobody give the answer: std::istringstream. This class is designed to build strings of unknown length by appending segments to the buffer (stream) just like you would use std::cout. The code would look like:

                            // Use a character buffer for reading characters from the socket.
                            // Make sure that a '\0' follows the character sequence, otherwise the behaviour
                            // of the istringstream class is undefined.
                            std::istringstream istrInputString;
                            char cBuf[100] = {0};
                            while( /* there are bytes from your socket */
                            {
                            /* read bytes from socket and appen terminating null character. */
                            istrInputString << cBuf;
                            }

                            // Do something with the string.

                            See MSDN[^] for more details. Hope this helps. :) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                            T Offline
                            T Offline
                            Tim Smith
                            wrote on last edited by
                            #14

                            But you will need to make sure cBuf is null terminated. Using std::vector with push_back or append will work better. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                            B 1 Reply Last reply
                            0
                            • S Smith

                              How do i define a space for unknown char size? i dont want to use like char str[50] or char str=new char(100); size should be infinite and adjust accordingly. if i enter "hi" , it should be of two only bytes. i'm trying winsock a program, so if the user should be allowed to enter any number of characters. so how?? regards, Rookie Installing MFC...2% complete

                              S Offline
                              S Offline
                              Stephen Hewitt
                              wrote on last edited by
                              #15

                              I have to agree with vipinasda here. Use CString (MFC) or std::string (STL) and save yourself a lot of hastles. My preference is to use STL where possible. Steve

                              1 Reply Last reply
                              0
                              • T Tim Smith

                                But you will need to make sure cBuf is null terminated. Using std::vector with push_back or append will work better. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                B Offline
                                B Offline
                                Bob Stanneveld
                                wrote on last edited by
                                #16

                                Hello, Since he indicated that he was using normal characters, there is no need to use vectors as this complicates the matter of displaying strings. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups