如何使用重定向C语言为文件输入 [英] How to use redirection in C for file input

查看:927
本文介绍了如何使用重定向C语言为文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要来自终端的文件,我知道该命令将如下:

I need to get the file from the terminal, I know the command will look like:

./a.out < fileName.txt

我不知道如何在我的程序中使用来自终端请求的文件中使用与fgets()。

I'm not sure how to use fgets() in my program to use the file requested from the terminal.

推荐答案

使用重定向将输入文件到标准输入的内容,所以你需要从你的code内部标准输入读取,所以像(检查省略错误为清楚起见)

Using redirection sends the contents of the input file to stdin, so you need to read from stdin inside your code, so something like (error checking omitted for clarity)

#include <stdio.h>

#define BUFFERSIZE 100

int main (int argc, char *argv[])
{
    char buffer[BUFFERSIZE];
    fgets(buffer, BUFFERSIZE , stdin);
    printf("Read: %s", buffer);
    return 0;
}

这篇关于如何使用重定向C语言为文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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