命令行参数和文件输入 [英] Command Line Arguments and File Input

查看:92
本文介绍了命令行参数和文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可怕的时间与命令行参数的最后一个学期文件输入,我需要利用它为我工作的练习。我有codeD一个简单的shell只是为了得到它的工作:

I had a terrible time with file input from command line arguments last semester and I need to utilize it for an exercise that I am working on. I have coded a simple shell just to get it working:

prob_5.c

#include <stdio.h>

int main(int argc, char *argv[]) {
int i;
FILE *fp;
int c;

for (i = 1; i < argc; i++) {
    fp = fopen(argv[i], "r");

    if (fp == NULL) {
        fprint(stderr, "cat: can't open %s\n", argv[i]);
        continue;
    }

    while ((c = getc(fp)) != EOF) {
        putchar(c);
    }

    fclose(fp);
}

return 0;
}

我似乎无法记住的命令是从从命令行调用我的程序。我曾尝试:

I can't seem to remember what the commands are for invoking my program from the command line. I have tried:

gcc -o prob_5 -g -ansi prob_5.c

我已经重新格式化,因为上学期我的电脑,所以也许我缺少一个系统路径?

I have reformatted my computer since last semester, so perhaps I am missing a System Path?

推荐答案

它看起来像你的程序只是希望有一个说法:文件名。您也可以先编译它。

It looks like your program just expects one argument: the file name. You also have to compile it first.

$ gcc -o prob_5 prob_5.c
$ ./prob_5 input_file.txt

如果它没有编译,那么你有另外一个问题。什么是当你执行 GCC

If it is not compiling, then you have another issue. What is returned when you execute gcc?

这篇关于命令行参数和文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆