简体   繁体   English

输入重定向不起作用?

[英]Input Redirection not working?

My instructions: Write a program that repeatedly (until end-of-file) reads in a character from the input stream. 我的指令:编写一个程序(直到文件结尾)反复(从文件结束)读取输入流中的字符。 If the character is upper case, change it to lower case and write it to the output stream. 如果字符是大写字母,请将其更改为小写并将其写入输出流。 For all other characters, write the character unchanged to the output stream. 对于所有其他字符,将不变的字符写入输出流。 Use getchar() for input, Use putchar() for output, and use input redirection for connecting the input file to the program 使用getchar()作为输入,使用putchar()作为输出,并使用输入重定向将输入文件连接到程序

My project name is Input and my textfile is input.txt. 我的项目名称是Input,我的文本文件是input.txt。 When I run it I type "Input < input.txt" The program just mimics that on the command window though so how do I get it to read from the text file? 当我运行它时,我键入“ Input <input.txt”,尽管该程序只是模仿了命令窗口中的内容,所以如何从文本文件中读取它呢?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
  char c, x;


   c=getchar();

  while (c != EOF)
 {

    c=getchar();
     x = tolower(c);
     if (isupper(c))
    {
        putchar(x);

    }
    else 
    {
        putchar(c);

    }

 }
    system("Pause");
}

I believe the problem is that you do not want to go to the program and type in Input < input.txt . 我相信问题是您不想转到该程序并Input < input.txt Instead, you want to open a terminal program, change directory into the directory containing the project, and then run the program from the command line by writing Input < input.txt . 而是要打开终端程序,将目录更改为包含项目的目录,然后通过编写Input < input.txt从命令行运行该程序。 This starts up the program and uses the contents of input.txt as the standard input stream, rather than reading text from the console. 这将启动程序,并使用input.txt的内容作为标准输入流,而不是从控制台读取文本。

That said, there are two bugs in your code. 也就是说,您的代码中有两个错误。 First, note that you have the line 首先,请注意您已经

c = getchar();

outside of your loop that does the input reading. 在循环外部进行输入读取。 You then immediately call 然后,您立即致电

c = getchar();

again inside the loop. 再次进入循环。 This means that you are discarding the very first character that you read in. 这意味着您将丢弃读入的第一个字符。

Second, your loop runs one more time than it needs to. 其次,您的循环运行的时间比需要的多。 If the second call to getchar() returns EOF , you do not detect this until after the current loop iteration finishes. 如果对getchar()的第二次调用返回EOF ,则直到当前循环迭代完成后才能检测到此错误。 This is because your check for EOF is at the top of the loop, which isn't reached until after you've already printed out the EOF character. 这是因为对EOF的检查位于循环的顶部,直到您已经打印出EOF字符后才可以进行检查。 To fix this, consider using the loop-and-a-half idiom and breaking out of the loop in the middle: 要解决此问题,请考虑使用“半个循环”习语并在中间跳出循环:

for (;;) {
    /* Read data. */
    if (/* no data left */) break;

    /* Process data. */
}

Stylistically, are you sure that you need both the if and else branches here? 从风格上讲,您确定在这里需要ifelse分支吗? Recall that tolower will not change the values of characters that aren't upper-case, so having one case for upper-case letters and one for lower-case is redundant. 回想一下, tolower不会更改不是大写字母的字符的值,因此对于大写字母使用一种大写字母,对于小写字母使用一种大写字母是多余的。 You can just output the character produced by tolower without the special cases. 您可以仅输出tolower产生的字符,而无需特殊情况。

Hope this helps! 希望这可以帮助!

The idiomatic way of doing this for ASCII text only is: 仅针对ASCII文本执行此操作的惯用方式是:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    int c;

    while((c = getchar()) != EOF)
        putchar(tolower(c));

    exit(EXIT_SUCCESS);
}

As textbooks are so fond of saying, UTF-8 is left as an easy exercise to the reader ;-) 正如教科书所说的那样, UTF-8留给读者一个简单的练习 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM