code_junkie

How to copy a few chars from a char[] to a char* in C?

Nov 13th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. char buffer[512] = "GET /testfile.htm HTTP/1.0";
  2. char* filename; // I want *filename to hold only "/testfile.htm"
  3.  
  4. msgLen = recv(connecting_socket, buffer, 512, 0);
  5. strncpy(filename, buffer+5, msgLen-9);
  6.  
  7. strncpy(filename, buffer+5, msgLen-9);
  8.  
  9. char filename[512];
  10.  
  11. char buffer[512] = "GET /testfile.htm HTTP/1.0";
  12. char filename[512]; // I want *filename to hold only "/testfile.htm"
  13.  
  14. msgLen = recv(connecting_socket, buffer, 512, 0);
  15. strncpy(filename, buffer+4, msgLen-4-9);
  16. //the first parameter should be buffer+4, not 5. Indexes are zero based.
  17. //the second parameter is count, not the end pointer. You should subtract
  18. //the first 4 chars too.
  19.  
  20. filename[msgLen-4-9] = 0;
  21.  
  22. memcpy(filename, buffer+4, msgLen-4-9);
  23. fileName[msgLen-4-9] = 0;
  24.  
  25. char* filename;
  26.  
  27. char filename[SOME_BUFFER_SIZE];
  28.  
  29. char filename[512]
  30.  
  31. filename = (char *)malloc(msgLen - 9 - 6 + 1 ); /* + 1 for the terminating null */
  32.  
  33. filename = (char*)malloc(512*sizeof(char));
Add Comment
Please, Sign In to add comment