Search code examples
cstringparsingstrchr

Having trouble parsing "file server commands" in C


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?


Solution

  • 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 *.