string to int

This is a discussion on string to int within the C++ Programming forums, part of the General Programming Boards category; OK last question: Code: unsigned int j = 0; do { if (recv(h,&buf[j],1,0)==SOCKET_ERROR) { addlog("retlinehttp(): recv() Failed! %d", WSAGetLastError()); return ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    string to int

    OK last question:

    Code:
       		unsigned int j = 0;
    		do { 
    			if (recv(h,&buf[j],1,0)==SOCKET_ERROR) {
    				addlog("retlinehttp(): recv() Failed! %d", WSAGetLastError());
    				return -2;
    			}
    			if (data = strstr(buf, "\r\n\r\n")) {
    				data2 = strstr(buf, "Content-Length:");
    				data2+=15;
    				break;
    			}
    			//fwrite (&buf[j] , 1 , 1 , file);
    			j++;
    		} while (j < sizeof(buf));
    		buf[j-1]='\0';
    		
    		int size = atoi(data2);
    
    		char* buffer;
    		buffer = new char[size];
    
       		unsigned int k = 0;
    		do { 
    			if (recv(h,&buffer[k],1,0)==SOCKET_ERROR) {
    				addlog("retlinehttp(): recv() Failed! %d", WSAGetLastError());
    				return -2;
    			}
    			fwrite (&buffer[k] , 1 , 1 , file);
    			k++;
    		} while (k < sizeof(buffer));
    At this point somthing went wrong buffer = new char[size];
    Lets say I download a file then I get back the information Content-Length: 24576 this value I have as a char and the pointer data2 points to it. Then I convert the char into int. Now the value is in size. But when I try to create a new buffer, the buffer is not big as size, but has a sizeof 4?
    Whats wrong?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    sizeof returns the size in bytes of the buffer variable (not the value of the contents of said variable) which is 4 because buffer is a 4-byte pointer variable. What you probably want is to use the value in the variable size again in your loop instead:

    Code:
    int size = atoi(data2);
    
    char* buffer;
    buffer = new char[size];
    
    unsigned int k = 0;
    do { 
        if (recv(h,&buffer[k],1,0)==SOCKET_ERROR) {
            addlog("retlinehttp(): recv() Failed! %d", WSAGetLastError());
            return -2;
        }
        fwrite (&buffer[k] , 1 , 1 , file);
        k++;
    } while (k < size);
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by hk_mp5kpdw
    sizeof returns the size in bytes of the buffer variable (not the value of the contents of said variable) which is 4 because buffer is a 4-byte pointer variable. What you probably want is to use the value in the variable size again in your loop instead:

    Code:
    int size = atoi(data2);
    
    char* buffer;
    buffer = new char[size];
    
    unsigned int k = 0;
    do { 
        if (recv(h,&buffer[k],1,0)==SOCKET_ERROR) {
            addlog("retlinehttp(): recv() Failed! %d", WSAGetLastError());
            return -2;
        }
        fwrite (&buffer[k] , 1 , 1 , file);
        k++;
    } while (k < size);

    WUHU works perfect
    But I don't understand why this works

    } while (j < sizeof(buf));

    same as the

    } while (k < sizeof(buffer));

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Because a char array is different from a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21