在C中,一次保存一个字符
我正在处理一个字符串,其中每个单词都用空格分隔。 <<表示是输入重定向,>表明这是一个输出重定向。 例如:
< 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了帮助您开始,假设您被允许修改
buff
,并假设一个简单的情况(最多一个<
和最多一个> > )。
首先,获取
<
和>
的位置,然后人为地分割字符串:
此时,
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>
Then you artificially split the string:
At this point,
buff
points to a C-terminated string that has no<
or>
.in
is eithernull
, or points to a string that follows<
and contains no>
. Same forout
.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..
so the path between < and > will be in filename.
and
假设您的输入模式固定为
<输入名称> output_name
并且您希望能够分别提取input_name
和output_name
。一种解决方案是使用“<>”拆分 str。以下代码将连续打印出 Hello , World 。将它们保存到
in
和out
留给您作为练习:)supposing your input patter is fixed as
< input_name > output_name
and you want to be able to extractinput_name
andoutput_name
respectively.One solution is to split the str using " <>". Following code will print out Hello , World consecutively. Saving them to
in
andout
is left for you as an exercise :)您需要决定两个文件名的存储空间分配在哪里,以及如何知道提供了多少存储空间。
此代码只是假设您为
in
和out
提供了足够大的字符串。如果你想更安全,那么你可以告诉函数每个目标字符串有多大。您可以压缩该代码。您可能决定返回重定向次数。您可能决定应该了解双输出重定向或双输入重定向。您可能决定应该返回一个位掩码来指示存在哪些重定向。使用相当复杂的接口,您也许能够指示输入行的哪些部分代表 I/O 重定向;这将有助于调用函数决定现在应该忽略该行的哪些部分。您可以像这样使用上面的代码:
You need to decide where the storage for the two filenames is allocated, and how you will know how much storage is provided.
This code simply assumes that you provide big enough strings for
in
andout
. 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: