在C中,一次保存一个字符

发布于 2024-12-07 06:45:30 字数 625 浏览 3 评论 0原文

我正在处理一个字符串,其中每个单词都用空格分隔。 <<表示是输入重定向,>表明这是一个输出重定向。 例如:

< Hello > World

我想将单词保存在不同的变量中(char *in,char *out) 我怎样才能做到这一点?我查看了字符串库,似乎没有一个能够完成这项工作。

到目前为止,我对这个问题的了解如下。

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. 
}

非常感谢。

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 (char *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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

得不到的就毁灭 2024-12-14 06:45:31

为了帮助您开始,假设您被允许修改 buff,并假设一个简单的情况(最多一个 < 和最多一个 > > )。

首先,获取<>的位置

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

,然后人为地分割字符串:

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

此时,buff指向一个C-没有 <> 的终止字符串。 in 要么为 null,要么指向 < 后面且不包含 > 的字符串。 out 也是如此。

然后,您需要“剥离”所有这些字符串(删除空格),并检查之后它们是否仍然包含有意义的数据。

您将需要更多的逻辑来正确处理所有情况,包括拒绝无效输入。

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.

童话里做英雄 2024-12-14 06:45:31

你可以使用这个..

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

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

所以<之间的路径和>将在文件名中。

 output = str2 + 1;

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;
愚人国度 2024-12-14 06:45:31

假设您的输入模式固定为 <输入名称> output_name 并且您希望能够分别提取 input_nameoutput_name

一种解决方案是使用“<>”拆分 str。以下代码将连续打印出 Hello , World 。将它们保存到 inout 留给您作为练习:)

    #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;
}

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;
}
好听的两个字的网名 2024-12-14 06:45:31

您需要决定两个文件名的存储空间分配在哪里,以及如何知道提供了多少存储空间。

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);
}

此代码只是假设您为 inout 提供了足够大的字符串。如果你想更安全,那么你可以告诉函数每个目标字符串有多大。您可以压缩该代码。您可能决定返回重定向次数。您可能决定应该了解双输出重定向或双输入重定向。您可能决定应该返回一个位掩码来指示存在哪些重定向。使用相当复杂的接口,您也许能够指示输入行的哪些部分代表 I/O 重定向;这将有助于调用函数决定现在应该忽略该行的哪些部分。

您可以像这样使用上面的代码:

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...
}

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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文