Thread: Problem with Strings

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    Problem with Strings

    Is there a way just put a few characters into a string instead of copying everything of a string into another string. Heres an example that im working on.

    Code:
    strcpy(net_data,"");
    			 strcpy(net_data_buffer,"");
    		  while(data_char2[p]!='.')
    		  {
    			  net_data_buffer[start]=data_char2[p];
    			  start++;
    			  p++;
    
    		  }
    		  net_data_buffer[start]='\n';
    
              strncpy(net_data,net_data_buffer,start );
    		  start=0;
    		  p++;
    however, the problem is that when i look at the value of net_data, there are the two letters that i want in there, and then there is garbage followed after that, even after i did this

    Code:
    strcpy(net_data," ");

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could either do:
    Code:
    strncpy(net_data,net_data_buffer,start );
    net_data[start] = '\0';
    Or:
    Code:
    strcpy(net_data,"");
    strncat(net_data,net_data_buffer,start );

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    cool thanks! it works now. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM