开发者

In C, Save one char at a time

开发者 https://www.devze.com 2023-04-09 09:23 出处:网络
I am processing a string in which each word is separated by spaces. The < indicates it is a input redirection, and > indicates it is a output redirection.

I am processing a string in which each word is separated by spaces. The < indicates it is a input redirection, and > indicates it is a output redirection. Ex:

< Hello > World

I want to save the words in different variables (c开发者_Python百科har *in, char *out ) How can I do that? I've looked through the string library and none seems to be able to do the job.

Here's what I have so far concerning this question.

char buff[MAXARGS];
char *str;
char *in;
char *out;

if( strchr(buff, '<') != NULL )
{
  str = strchr(buff, '<');
  str++;
  if( *str == ' ' || *str == '\0' || *str == '\n'  || *str == '\t' )
  {
     fprintf( stdout, "User did not specify file name!\n" );
  }
  else
      in =  str; // This will save Hello > World all together. I don't want that. 
}

Thanks much.


To get you started, here's how you could do it assuming you are allowed to modify buff, and assuming a simple case (at most one < and at most one >).

First, get the position of the < and >

in  = strchr(buff, '<');
out = strchr(buff, '>');

Then you artificially split the string:

if (in) {
  *in = 0;
  in++;
}
if (out) {
  *out = 0;
  out++;
}

At this point, buff points to a C-terminated string that has no < or >. in is either null, or points to a string that follows < and contains no >. Same for out.

Then you need to "strip" all these strings (remove whitespace), and check that after that they still contain meaningful data.

You'll need a whole lot more logic to get all the cases right, including rejecting invalid input.


u can use this..

char filename[max_path]
 str1 = strchr(buff, '<');
 str2 = strchr(buff, '>');

 memcpy( filename , str1+1 , str2-str1-1 ); 

so the path between < and > will be in filename.

and

 output = str2 + 1;


supposing your input patter is fixed as < input_name > output_name and you want to be able to extract input_name and output_name respectively.

One solution is to split the str using " <>". Following code will print out Hello , World consecutively. Saving them to in and out is left for you as an exercise :)

    #include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="< Hello > World";
  char *in;
  char *out;
  char *pch;
  char *del=" <>";
  pch = strtok (str,del);
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, del);
  }
  return 0;
}


You need to decide where the storage for the two filenames is allocated, and how you will know how much storage is provided.

static void find_filename(const char *str, char *name)
{
    char c;
    while ((c = *str++) != '\0' && isspace((unsigned char)c))
        ;
    if (c != '\0')
    {
        *name++ = c;
        while ((c = *str++) != '\0' && !isspace((unsigned char)c))
            *name++ = c;
    }
}

int find_io_redirection(const char *str, char *in, char *out)
{
    const char *lt = strchr(str, '<');
    const char *gt = strchr(str, '>');
    if (lt != 0)
        find_filename(lt+1, in);
    if (gt != 0)
        find_filename(gt+1, out);
    return(lt != 0 && gt != 0);
}

This code simply assumes that you provide big enough strings for in and out. If you want to be safer, then you tell the function(s) how big each target string is. You can compress that code. You might decide you want to return the number of redirections. You might decide you should know about double output redirections, or double input redirections. You might decide you should return a bit mask indicating which redirections were present. With a considerably more complex interface, you might be able to indicate which parts of the input line represented the I/O redirection; this would help in the calling function to decide which parts of the line should now be ignored.

You'd use the code above like this:

char buffer[MAXCMDLEN];
char in[MAXFILENAMELEN];
char out[MAXFILENAMELEN];

if (fgets(buffer, sizeof(buffer), stdin) != 0)
{
    if (find_io_redirection(buffer, in, out))
        ...process buffer know that I/O redirection is present...
    else
        ...witter about missing I/O redirection...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消