C - 在命令行中使用尖括号

[英]C - Using angle brackets in command lines


At the moment I have this code:

目前我有这个代码:

int main(int argc,char* argv[]) {
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    char * txtfile = argv[1];

    fp = fopen(txtfile, "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);

    while ((read = getline(&line, &len, fp)) != -1) {
        printf("Retrieved line of length %zu :\n", read);
        printf("%s", line);
    }

    fclose(fp);
    if (line)
    free(line);
    exit(EXIT_SUCCESS);

    return 0;
}

It's function is to read a file line by line and print its contents along with the number of the number of characters on that line but however when I try to pass angle brackets such as '>' or '<', it terminates and it doesn't give me any output. I have a task where I'm required to take it as a command line argument namely in the form of:

它的功能是逐行读取文件并打印其内容以及该行上的字符数,但是当我尝试通过诸如“>”或“<”的尖括号时,它会终止并且它不会不给我任何输出。我有一个任务,我需要将其作为命令行参数,即以下列形式:

./hello < output.txt 

to read a file.

阅读文件。

Note, the spec I was given says that my code will be tested in that specific manner, I'm just wondering how I could use the "<" without the quotations. (added OPs comment for clarity on the unchangeable requirment.)

请注意,我给出的规范说我的代码将以特定的方式进行测试,我只是想知道如何使用没有引号的“<”。 (为了澄清不可改变的要求,增加了OP评论。)

Is there any way I can work around this problem? Any tips would be greatly appreciated!

有什么办法可以解决这个问题吗?任何提示将非常感谢!

4 个解决方案

#1


2  

You do not have a problem to work around.
Your code will be tested by feeding it the content of output.txt as input. That will be exactly as if a user types in the input, to become standard input to all ways of reading input. This is called "input redirection".
So all your ways to read in input (e.g. scanf()) will receive text from that file.

你没有问题可以解决。您的代码将通过输入output.txt的内容作为输入进行测试。这将完全像用户输入输入,成为所有读取输入方式的标准输入。这称为“输入重定向”。因此,您在输入中读取的所有方式(例如scanf())都将从该文件接收文本。

Presumeably your program will have the goal to read something from user and react to that input in a specified way. If that is not true, then please give an example of one of your programming assignments and explain why using scanf() (or something else) is not applicable to access the test input.

假设您的程序的目标是从用户读取内容并以指定的方式对该输入作出反应。如果不是这样,那么请举例说明您的一个编程分配,并解释为什么使用scanf()(或其他东西)不适用于访问测试输入。

To put it differently, if your problem is that you cannot access the "command line parameters" (which actually are not command line parameters, so you cannot access them as such), then try this:
Make a simple input forwarder (read input and just print if) with a file of that name existing and having some "hello input redirection world" input. Build and run it from commandline as specified.
Does it show the content of the file "output.txt"? I expected so.
Wouldn't that be the workaround for not having the expected command line parameters?

换句话说,如果您的问题是您无法访问“命令行参数”(实际上不是命令行参数,因此您无法访问它们),请尝试以下操作:创建一个简单的输入转发器(读取输入和只需打印if),该文件具有该名称,并且具有一些“hello input redirection world”输入。根据指定从命令行构建并运行它。它是否显示文件“output.txt”的内容?我是这么想的。这不是没有预期的命令行参数的解决方法吗?

Picking up the recommendation by Jonathan Leffler, check whether there is a non-NULL argv[1] argument. If there is none, then study the concept of input-redirection to find out where the input might be which you need for your program.

拿起Jonathan Leffler的建议,检查是否存在非NULL argv [1]参数。如果没有,那么请研究输入重定向的概念,以找出您的程序所需的输入位置。

#2


1  

Try:

./hello "<" > output.txt

./hello“<”> output.txt

It will pass < as commad line arg and output wll be redirected to output.txt.

它将传递 <作为commad行arg,输出将被重定向到output.txt。< p>

#3


0  

Try single quotes, like this:

试试单引号,如下所示:

./hello '<' output.txt 

Single quote inhibits all interpretation of a sequence of characters.

单引号禁止对字符序列的所有解释。

#4


0  

the following proposed code:

以下提议的代码:

  1. cleanly compiles
  2. performs the desired functionality
  3. 执行所需的功能

  4. outputs the results from each call to getline()
  5. 输出每次调用getline()的结果

And now the proposed code:

现在建议的代码:

#include <stdio.h>
#include <stdlib.h>


int main( void )
{
    char * line = NULL;
    size_t len = 0;
    ssize_t getlineStatus;


    while ((getlineStatus = getline(&line, &len, stdin) != -1) )
    {
        printf("Returned value from getline(): %ld :\n", getlineStatus);
        printf( "number of bytes allocated for line: %lu\n", len );
        printf("%s\n\n", line);
        free( line );
        line = NULL;
        len = 0;
    }

    free( line );
    return 0;
}

I'm running ubuntu linux 16.04. using the source file for the input:

我正在运行ubuntu linux 16.04。使用源文件进行输入:

untitled2 < untitled2.c

results in:

Returned value from getline(): 1 :
number of bytes allocated for line: 120
#include <stdio.h>


Returned value from getline(): 1 :
number of bytes allocated for line: 120
#include <stdlib.h>


Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120
int main( void )


Returned value from getline(): 1 :
number of bytes allocated for line: 120
{


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //FILE * fp;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    char * line = NULL;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    size_t len = 0;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    ssize_t getlineStatus;


Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //char * txtfile = argv[1];


Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //fp = fopen(txtfile, "r");


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //if (fp == NULL)


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //    exit(EXIT_FAILURE);


Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120
    while ((getlineStatus = getline(&line, &len, stdin) != -1) )


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    {


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        printf("Returned value from getline(): %ld :\n", getlineStatus);


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        printf( "number of bytes allocated for line: %lu\n", len );


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        printf("%s\n\n", line);


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        free( line );


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        line = NULL;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
        len = 0;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    }


Returned value from getline(): 1 :
number of bytes allocated for line: 120



Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //fclose(fp);


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //if (line)


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    //    free( line );


Returned value from getline(): 1 :
number of bytes allocated for line: 120
    return 0;


Returned value from getline(): 1 :
number of bytes allocated for line: 120
}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2018/04/03/7206410ab89159fc5cf8c385b6573063.html



猜您在找
在shell脚本中使用括号/ parantheses与在命令行bash中使用它们 - Using brackets/parantheses inside shell scripts vs using them from the command line bash 在Windows命令提示符中转义尖括号 - Escape angle brackets in a Windows command prompt 使用带尖括号和引号的fwrite - Using fwrite with angle brackets and quotation marks and quote marks “ ”">使用尖括号< >和引号" "导入 - #import using angle brackets < > and quote marks “ ” object - c类接口中的尖括号“<…>”用于什么? - What are the angle brackets “<…>” in an Obj-C class interface for?
智能推荐
 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告