目前我有这段代码:
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;
}
它的功能是逐行读取文件并打印其内容以及该行上的字符数,但是当我尝试通过诸如“>”的尖括号时或'<',它终止,它不会给我任何输出。我有一个任务,我需要将其作为命令行参数,即:
./hello < output.txt
读取文件。
注意,我给出的规范说我的代码将以特定的方式进行测试,我只是想知道如何使用“&lt;”没有报价。 (为了澄清不可改变的要求,增加了OP评论。)
有什么方法可以解决这个问题吗?任何提示将不胜感激!
答案 0 :(得分:2)
你没有问题可以解决
您的代码将通过输入output.txt的内容作为输入进行测试。这将完全像用户输入输入,成为所有读取输入方式的标准输入。这称为“输入重定向”
因此,您阅读输入的所有方式(例如scanf()
)都将从该文件接收文本。
假设您的程序的目标是从用户读取内容并以指定的方式对该输入做出反应。如果不是这样,那么请举一个你的编程作业的例子,并解释为什么使用scanf()
(或其他东西)不适用于访问测试输入。
换句话说,如果您的问题是您无法访问“命令行参数”(实际上不是命令行参数,因此您无法访问它们),请尝试以下操作:
创建一个简单的输入转发器(读取输入,只打印if),该文件具有该名称的存在并具有一些“hello input redirection world”输入。根据指定从命令行构建并运行它
它是否显示文件“output.txt”的内容?我是这么想的。
这不是没有预期命令行参数的解决方法吗?
收集Jonathan Leffler的建议,检查是否存在非NULL argv [1]参数。如果没有,那么研究输入重定向的概念,找出你的程序所需的输入位置。
答案 1 :(得分:1)
尝试:
./ hello&#34;&lt;&#34; &GT; output.txt的
它将<
作为commad line arg传递,输出将被重定向到output.txt。
答案 2 :(得分:0)
尝试单引号,如下所示:
./hello '<' output.txt
单引号禁止对字符序列的所有解释。
答案 3 :(得分:0)
以下提议的代码:
getline()
现在建议的代码:
#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;
}
我正在运行ubuntu linux 16.04。使用源文件进行输入:
untitled2 < untitled2.c
结果:
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
}