0

The commands will be in the form:

command filename bytes\nfile_contents

I'm having no trouble separating ADD, filename, and the number of bytes, but I am not sure how I can get the remainder of the server command, namely the file contents.

This is how I am parsing each command. Currently to get the file contents I am getting the left most char* that is '\n'. the file_contents char* never changes from NULL.

command = strtok( message, " \n" );
file = strtok( NULL, " " );
bytes = atoi( strtok( NULL, "\n" ) );
file_contents = strchr( message, '\n' );

Any suggestions for how to get the file contents?

Jonathan Wrona
  • 423
  • 2
  • 7
  • 19
  • Are you sure you have a true newline (ASCII 10 0xa) embedded in bytes`\n`file_contents or is that simply a `backslash` followed by `n`? – David C. Rankin Nov 14 '14 at 21:55
  • 1
    If it is a newline, it is consumed by your `bytes` call, causing `file_contents = strchr( message, '\n' );` to point to the newline at the end of message (if present). Using `strtok` again should suffice `file_contents = strtok( NULL, "\n" );` (ending on the newline or null-terminating char (if no newline present). – David C. Rankin Nov 14 '14 at 21:58

1 Answers1

0

I would do

command = strtok( message, " \n" );
file = strtok( NULL, " " );
bytestr = strtok( NULL, "\n" );
bytes = atoi( bytestr );
file_contents = bytestr + strlen(bytestr) + 1;

assuming that file_contents and bytestr are both char *.

user3386109
  • 34,287
  • 7
  • 49
  • 68