C语言中低级标准在接受名然后打印文件内容到标准输出 [英] C low-level standard-in to accept filename then printing file contents to stdout

查看:253
本文介绍了C语言中低级标准在接受名然后打印文件内容到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过标准输入从用户获取文件名,打开用的open()文件并将其分配给一个文件描述符,然后打印文件内容到标准输出。这是我的code,并且它不能正常工作。

问题:


  1. 的的printf(请输入文件名);声明永远不会显示出来

  2. 它永远不会打开文件;而不是不管用户输入被打印在屏幕上,然后在打印的没有这样的文件或目录错误信息并退出程序

  3. 在终端
  4. 的程序存在后,我看到输入文件名打印前提示

code:

  {
        的printf(请输入文件名:);
        阅读(STDIN_FILENO,userInput,sizeof的(userInput));
        如果((input_file1 =开(userInput,O_RDONLY))小于0)
        {
            PERROR(userInput);
            出口(1);
        }        而((N =读(input_file1,缓冲器,的sizeof(缓冲液)))0)
        {
            如果((写(STDOUT_FILENO,缓冲器,正))小于0)
            {
                PERROR(无法写到标准输出);
                关闭(input_file1);
                出口(1);
            }
        }
    }

控制台:

  {机} user1的168:LS //显示该文件存在
a.out的备份文件1
机{} user1的170:./a.out
文件1 //这是用户输入
文件1 //这是打印的无缘无故
: 无此文件或目录 // ????
输入文件名:{机器} user1的171://现在打印的提示...?


解决方案

&LT; stdio.h中&GT; 输入/输出例程的缓冲(见的 STDIO(3)&安培; 则setbuf (3),你需要调用 fflush(3)(最近的libc,它是隐式调用了标准输出如果您使用与fgets 或<$ C $读C> scanf函数),而且你真的应该避免混淆文件描述符和文件处理在同一个输出或输入(见的的fileno(3)但总是叫 fflush ....)。
因此,替换

 的printf(请输入文件名:);
         阅读(STDIN_FILENO,userInput,sizeof的(userInput));

 的printf(请输入文件名:\\ n);
         fflush(NULL);
         如果(!与fgets(userInput,sizeof的(userInput),标准输入))
           {PERROR(与fgets);出口(EXIT_FAILURE); };

其实这里的 fflush 可如果你保持很重要的终止 \\ n (换行)来避免。如果你不希望任何新行,你应该更好地叫 fflush (但有些libc中呼吁为你)。

电话 fflush 过多或过于频繁的要少得多有害的(因为都已经刷新流是一个无操作),比调用它太少或不够的。

但你应该了解函数getline (3)(以避免固定长度线)。在Linux和GNU系统的readline 是值得一用的:它使您给一个性感的提示,并且用户编辑输入行。

I want to get a file name from a user via stdin, open the file with open() and assign it to a file descriptor, then print the contents of that file to stdout. This is my code, and it's not working properly.

Problems:

  1. the printf("enter filename"); statement is never showing up
  2. it never opens the file; instead whatever the user inputs is printed to the screen and then the "no such file or directory" error message is printed and the program exits
  3. after the program exists i see "enter filename" printed before the prompt in terminal

CODE:

    {
        printf("Enter the filename: ");
        read(STDIN_FILENO, userInput, sizeof(userInput));
        if((input_file1 = open(userInput, O_RDONLY)) < 0)
        {
            perror(userInput);
            exit(1);
        }

        while((n = read(input_file1, buffer, sizeof(buffer))) > 0)
        {
            if((write(STDOUT_FILENO, buffer, n)) < 0)
            {
                perror("failed to write to standard-out");
                close(input_file1);
                exit(1);
            }
        }
    }

Console:

machine{user1}168: ls // to show that the file exists
a.out  backup  file1 
machine{user1}170: ./a.out
file1 // this is user input
file1 // this is printed for no reason
: No such file or directory // ????
Enter the filename: machine{user1}171: // now the prompt is printed...? 

解决方案

<stdio.h> input/output routines are buffered (see stdio(3) & setbuf(3). You need to call fflush(3) (on recent libc, it is implicitly called for stdout if you read with fgets or scanf). And you really should avoid mixing file descriptors and FILE handles on the same output or input (see fileno(3) but always call fflush....). So replace

         printf("Enter the filename: ");
         read(STDIN_FILENO, userInput, sizeof(userInput));

with

         printf("Enter the filename: \n");
         fflush(NULL);
         if (!fgets(userInput,sizeof(userInput),stdin))
           { perror("fgets"); exit(EXIT_FAILURE); };

Actually here the fflush could be avoided if you keep the very important terminating \n (newline). If you don't want any newline you should better call fflush (but some libc are calling it for you).

Calling fflush too much or too often is much less harmful (because on all already flushed stream it is a no-op) than calling it too little or not enough.

But you should learn about getline(3) (to avoid fixed-length lines). On Linux and GNU systems readline is worth using: it enables you to give a sexy prompt, and your user to edit the typed line.

这篇关于C语言中低级标准在接受名然后打印文件内容到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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