简体   繁体   English

将命令行参数和文本文件传递给程序

[英]Passing a Command Line Argument and a textfile to a program

I have a Read and Reverse Coding assignment where I need to pass a Textfile and a character ( -L , or -W ), depending on whether the operator wants the textfile returned in reverse by lines or by words. 我有一个读取和反向编码分配,在该分配中,我需要传递一个文本文件和一个字符( -L-W ),具体取决于操作员是希望文本文件通过行还是单词反向返回。 (I should also note that the assignment requires that nothing is asked of the user during the code. It must be decided which variation is wanted in the command line.) (我还应该注意,该分配要求在代码执行过程中,用户什么都不需要。必须确定在命令行中需要哪种变体。)

I don't need help with the code to reverse the lines or words, but do need help with understanding how to take in character and the textfile, then use them in the code. 我不需要代码来反转行或单词的帮助,但是需要了解如何接受字符和文本文件,然后在代码中使用它们的帮助。 I've tried using the parameters (int argc, char *argv[]) on the main , but anytime I try to pass in just the -L the terminal either says Command not found or clang: error: argument to '-L' is missing (expected 1 value) 我使用的参数尝试(int argc, char *argv[])main ,但是只要我尝试在刚刚通过-L终端或者说Command not foundclang: error: argument to '-L' is missing (expected 1 value)

Also, when my teacher passes a textfile to a program he often uses a > . 另外,当我的老师将文本文件传递给程序时,他经常使用> Can someone explain how to use this? 有人可以解释如何使用吗?

Ex. 防爆。 program.c > hello.txt

Then he would end up using that .txt in the program. 然后,他最终将在程序中使用该.txt

Consider this: 考虑一下:

program -L < data.txt

or 要么

program -W < data.txt

or 要么

cat data.txt | program -L

The "-L" or "-W" will be in argv[1] . “ -L”或“ -W”将在argv[1]

Good luck! 祝好运!

The idea of Passing a Command Line Arguement is the following 传递命令行参数的想法如下

Argc : argument counter amount of "strings"(arguments) passed for execution. Argc :传递给执行的“字符串”(参数)的参数计数器数量。 Its always 1 or greater as the calling of the function is an argument. 它始终为1或更大,因为函数的调用是自变量。

Argv : argument vectors(pointers), is a pointer to each of the arguments received by the command line Argv :参数向量(指针),是指向命令行接收的每个参数的指针

Example of program call: 程序调用示例

./myprogram -w ./myprogram -w

argc=2 的argc = 2

argv will have two pointers to strings(char): argv将具有两个指向string(char)的指针:

argv[0]= "./myprograms" argv [0] =“ ./myprograms”

argv[1]= "-w" argv [1] =“ -w”

Now with your problem 现在有你的问题

When excecuting a program via command line you have a lot of options amongst these: 通过命令行执行程序时,其中有很多选择:

1) One of these is to give the program input of a file(the file will be passed character by character to the standard input ending with an EOF or -1 -not an ascii character-) These can be done by the follow way 1)其中之一是提供文件的程序输入(文件将逐字符传递到以EOF或-1结尾的标准输入-而不是ascii字符-)可以通过以下方式完成

./program.c < hello.txt ./program.c <hello.txt

2)Redirect the output of the program to a file 2)将程序的输出重定向到文件

./program.c > hello.txt ./program.c> hello.txt

What you are looking to do is input a file while passing an argument this can be done the following way 您要做的是在传递参数的同时输入文件,这可以通过以下方式完成

./program.c < hello.txt -L

IMPORTANT : "< hello.txt" will NOT count as an argument so for this case the case the argc and argv will be the follow 重要说明 :“ <hello.txt”将不被视为参数,因此在这种情况下,将使用argc和argv

argc=2 的argc = 2

argv[0]="./program.c" 的argv [0] = “./ program.c”

argv[1]="-L" 的argv [1] = “ - L”

Hopes this helps comment if you need anymore help or something isn't clear. 希望这有助于在您需要更多帮助或不清楚的地方发表评论。 Good luck with your course!!! 祝您课程顺利!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM