传递文本文件而不是用户输入作为命令行参数
Passing text file instead of user input as command line argument
我正在尝试弄清楚如何能够将 .txt 文件作为命令提示符参数而不是用户输入来读取。如果我有程序
#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;
}
其中文件 "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;
}
我可以运行这个程序
./program 3 5
输出将是
sum = 3 + 5 = 8
但是如果我有一个文件 (textfile.txt) 只包含列表形式的相同数字,即
3
5
并尝试运行它作为
./program < textfile.txt
我明白了
Segmentation fault
如果 textfile.txt 包含“3 5”,它会做同样的事情,尽管无论如何我使用列表形式的文本文件很重要。我需要更改什么才能使“./program textfile.txt”具有与“./program 3 5”相同的输出?
我猜问题出在 int 主括号之间,我只是不确定具体是什么。
您将 textfile.txt 重定向到您的程序中,因此无法直接从参数中读取它。
您可以使用 cin 从重定向中读取文件内容
char c;
while (std::cin.get(c))
{
// process char
}
或者,您可以将文件名作为参数传递,例如 ./program textfile.txt,然后从文件中读取
char* filename = args[1];
std::ifstream infile(filename);
int a = 0, b = 0;
infile >> a >> b;
运行 您的程序为:
./program myfile.txt
并使用 argv[1]
命令行参数来接受文件名。相应地解析:
#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';
}
}
测试程序
当你想检查这些东西时,我建议首先了解应用程序如何接收它们。我将从以下最小程序开始:
int main (int argc, char * argv[]) {
for (int i = 0; i < argc; i++)
cout << argv[i] << endl;
return 0;
}
并执行
./program 3 5
输出:
./program
3
5
./program < textfile.txt
输出:
./program
现在您可以看到问题出在如何将 txt
文件传递给应用程序。而您的应用程序崩溃的原因是因为您使用了参数而没有验证它们是否确实存在!
说明
符号 (<) 表示打开文件并将其附加到应用程序的标准输入 (cin)。
./program < textfile.txt
与参数无关,您必须从 cin
.
读取它
使用参数的解决方案
使用
./program `cat textfile.txt`
cat
命令将 "replace itself" 文件内容,就像您手动写入一样。
使用 < 和 cin 的解决方案
当 运行 ./program < textfile.txt
以下将打印数字:
int a, b;
cin >> a >> b;
cout << a << endl;
cout << b << endl;
组合解
这对两者都有效:
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;
}
#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;
}
我正在尝试弄清楚如何能够将 .txt 文件作为命令提示符参数而不是用户输入来读取。如果我有程序
#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;
}
其中文件 "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;
}
我可以运行这个程序
./program 3 5
输出将是
sum = 3 + 5 = 8
但是如果我有一个文件 (textfile.txt) 只包含列表形式的相同数字,即
3
5
并尝试运行它作为
./program < textfile.txt
我明白了
Segmentation fault
如果 textfile.txt 包含“3 5”,它会做同样的事情,尽管无论如何我使用列表形式的文本文件很重要。我需要更改什么才能使“./program textfile.txt”具有与“./program 3 5”相同的输出?
我猜问题出在 int 主括号之间,我只是不确定具体是什么。
您将 textfile.txt 重定向到您的程序中,因此无法直接从参数中读取它。
您可以使用 cin 从重定向中读取文件内容
char c;
while (std::cin.get(c))
{
// process char
}
或者,您可以将文件名作为参数传递,例如 ./program textfile.txt,然后从文件中读取
char* filename = args[1];
std::ifstream infile(filename);
int a = 0, b = 0;
infile >> a >> b;
运行 您的程序为:
./program myfile.txt
并使用 argv[1]
命令行参数来接受文件名。相应地解析:
#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';
}
}
测试程序
当你想检查这些东西时,我建议首先了解应用程序如何接收它们。我将从以下最小程序开始:
int main (int argc, char * argv[]) {
for (int i = 0; i < argc; i++)
cout << argv[i] << endl;
return 0;
}
并执行
./program 3 5
输出:./program 3 5
./program < textfile.txt
输出:./program
现在您可以看到问题出在如何将 txt
文件传递给应用程序。而您的应用程序崩溃的原因是因为您使用了参数而没有验证它们是否确实存在!
说明
符号 (<) 表示打开文件并将其附加到应用程序的标准输入 (cin)。
./program < textfile.txt
与参数无关,您必须从 cin
.
使用参数的解决方案
使用
./program `cat textfile.txt`
cat
命令将 "replace itself" 文件内容,就像您手动写入一样。
使用 < 和 cin 的解决方案
当 运行 ./program < textfile.txt
以下将打印数字:
int a, b;
cin >> a >> b;
cout << a << endl;
cout << b << endl;
组合解
这对两者都有效:
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;
}
#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;
}