Problem with Strings

This is a discussion on Problem with Strings within the C Programming forums, part of the General Programming Boards category; Is there a way just put a few characters into a string instead of copying everything of a string into ...

  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, 09: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, 01:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 09:05 PM

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