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
}

注意!

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



 
粤ICP备14056181号  © 2014-2020 ITdaan.com