程序C语言编程使用终端导入文本文件 [英] Import text file using terminal in program C programming

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

问题描述

我想用C编写一个程序,我会从终端运行,将接受输入例如从error.log中文件的一些行。我该怎么做?

I am trying to write a program in C that I will run from the terminal and will accept as input some lines from a file for example "error.log". How can I do that?

命令示例:
./prog< error.log中

Command Example: ./prog < error.log

推荐答案

这是例子开始。

#include <stdio.h>

#define BLOCK_SIZE 256

int main(int argc, char** argv)
{
    char buf[BLOCK_SIZE];
    size_t bytes;

    while(!feof(stdin)) {
        bytes = fread(buf, 1, BLOCK_SIZE, stdin);
        fwrite(buf, 1, bytes, stdout);
    }
    return 0;
}

它只是打印出您指定为 ./ PROG&LT文件; filetoprint.txt

这篇关于程序C语言编程使用终端导入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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