简体   繁体   English

将文本文件而不是用户输入作为命令行参数传递

[英]Passing text file instead of user input as command line argument

I'm trying to figure out how to be able to read in a .txt file as a command prompt argument instead user input.我试图弄清楚如何能够读取 .txt 文件作为命令提示符参数而不是用户输入。 If I have the program如果我有程序

#include <iostream>
#include "cmdline.h"
using namespace std;

int main (int cnt, char * args[]) {
    int a = cmdline_int(args, 1);
    int b = cmdline_int(args, 2);
    cout << "sum = " << a << " + " << b << " = " << a + b << endl;
    return 0;   
}

where the file "cmdline.h" contains其中文件“cmdline.h”包含

#include <cstdlib>
#include <string>
using namespace std;

int cmdline_int( char* cmdline_a[], int n ) {
  return atoi( cmdline_a[ n ] );
}
char cmdline_char( char* cmdline_a[], int n ) {
  char c = cmdline_a[ n ][0];
  return c;
}

I can run the program as我可以运行程序

./program 3 5 ./程序 3 5

and the output will be输出将是

sum = 3 + 5 = 8总和 = 3 + 5 = 8

however if I have a file (textfile.txt) that simply contains those same numbers in list form, ie但是,如果我有一个文件(textfile.txt),它只包含列表形式的相同数字,即

3
5

and try to run it as并尝试运行它

./program < textfile.txt ./program < 文本文件.txt

I get我得到

Segmentation fault分段错误

it does the same thing if textfile.txt contains "3 5", though its important that I use a text file in list form anyway.如果 textfile.txt 包含“3 5”,它会做同样的事情,尽管无论如何我使用列表形式的文本文件很重要。 What do I need to change to make "./program textfile.txt" have the same output as "./program 3 5" ?我需要更改什么才能使 "./program textfile.txt" 具有与 "./program 3 5" 相同的输出?
I'm guessing the problem lies between the int main parenthesis, I'm just not sure what specifically.我猜问题出在 int 主括号之间,我只是不确定具体是什么。

Test program测试程序

When you want to check such things, I would recommend first understanding how the application receives them.当你想检查这些东西时,我建议首先了解应用程序如何接收它们。 I would start with the following minimal program:我将从以下最小程序开始:

int main (int argc, char * argv[]) {
  for (int i = 0; i < argc; i++)
    cout << argv[i] << endl;
  return 0;   
}

And execute it并执行它

  • ./program 3 5 outputs: ./program 3 5输出:

     ./program 3 5
  • ./program < textfile.txt outputs: ./program < textfile.txt输出:

     ./program

So now you can see that the issue is with how the txt file is passed to the application.所以现在您可以看到问题在于如何将txt文件传递给应用程序。 And the reason your application crashes is because you use the arguments without validating that they actually exist!您的应用程序崩溃的原因是您使用了参数而没有验证它们是否确实存在!

Explanation说明

The symbol (<) means opening the file up and attaching it to the standard input (cin) of your application.符号 (<) 表示打开文件并将其附加到应用程序的标准输入 (cin)。

./program < textfile.txt is not related to the arguments, instead you have to read it from cin . ./program < textfile.txt与参数无关,您必须从cin读取它。

Solution using arguments使用参数的解决方案

Use使用

./program `cat textfile.txt`

The cat command will "replace itself" with the contents of the file and it will work as if you manually wrote it. cat命令将用文件的内容“替换自身”,它会像您手动编写它一样工作。

Solution using < and cin使用 < 和 cin 的解决方案

When running with ./program < textfile.txt the following will print the numbers:当使用./program < textfile.txt运行时,以下将打印数字:

int a, b;
cin >> a >> b;
cout << a << endl;
cout << b << endl;

Combined solution组合方案

This will work for both:这对两者都有效:

int main (int argc, char * argv[]) {
  int a, b;
  if (argc == 1) { // No extra arguments, read from cin
    cin >> a >> b;
  } else { // Use args
    a = cmdline_int(argv, 1);
    b = cmdline_int(argv, 2);
  }

  // Do something with a,b
  return 0;
}

Run your program as:运行你的程序:

./program myfile.txt

and use the argv[1] command line parameter to accept the file name instead.并使用argv[1]命令行参数来接受文件名。 Parse accordingly:相应地解析:

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]) {
    if (argc > 1) {
        std::ifstream fs(argv[1]);
        int a, b;
        while (fs >> a >> b) {
            std::cout << a << ' ' << b << '\n';
        }
    } else {
        std::cout << "No arguments." << '\n';
    }
}

You redirect the textfile.txt into your program, so you couldn't read it from the parameter directly.您将 textfile.txt 重定向到您的程序中,因此您无法直接从参数中读取它。

You could read the file content from the redirect by using cin您可以使用 cin 从重定向中读取文件内容

char c;
while (std::cin.get(c))
{
    // process char
}

Or, you could pass the filename as paramter like ./program textfile.txt , and read from the file或者,您可以将文件名作为参数传递,如./program textfile.txt ,并从文件中读取

char* filename = args[1];
std::ifstream infile(filename);
int a = 0, b = 0;
infile >> a >> b;
#include <stdio.h>
int main(int argc, char* argv[]) {
  *//read from textfile one char at a time:*
  char c;
  while ((c=getchar()) != EOF){ 
     *//Command line should look like:
     //./program < textfile.txt*
    printf("%c",c); //print char 
  }
  return 0;
}

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

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