【问题标题】:Input/Output through terminal with Codejam questions [closed]通过带有 Codejam 问题的终端输入/输出 [关闭]
【发布时间】:2013-04-07 19:09:48
【问题描述】:

在我开始我的问题之前,我想声明几件事:- 这不是本网站上已有问题的副本。 但是,在类似的行上碰巧有一个问题,我仍然不“确切地”理解输入输出是如何完成的。如,回答者说代码有

int main(int argc, int argv**)
{
}

但是,在网站上发布的解决方案中,甚至没有使用。一个例子就是问题https://code.google.com/codejam/contest/1460488/dashboard#s=p0&a=0。 (说方言。我确实使用 ubuntu 终端,并尝试按照How can I do file i/o without fstream for competitions like google code jam? 的指示进行操作。结果我的输出文件中没有写入任何内容。我可以更好地指导,如何做到这一点。这不是我所做的功课这已经。我只需要知道如何在 linux 终端中进行输入输出。

我也给出了我的代码:-

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define DEBUG 1
#define printd if (DEBUG) printf

char cipher[26] =     {'y', 'h', 'e', 's', 'o', 'c', 'v', 'x', 'd', 'u', 'i', 'g', 'l', 'b', 'k', 'r', 'z', 't', 'n', 'w', 'j', 'p', 'f', 'm', 'a', 'q'};  

int main()
{
  int count, j;
  scanf("%d\n", &count);
  for (j=0;j<count;j++)
  {
    printf("Case #%d: ",j+1);
    translate();
    printf("\n");
  }
}
void translate()
{
  char c;
  scanf("%c", &c);
  while (c != '\n')
  {
    if (c == ' ')
      printf(" ");
    else
    {
      int index = c - 'a';
      if (index >=0 && index <= 26)
      {
        printf("%c", cipher[index]);
      }
    }
    scanf("%c", &c);
  }
}

谢谢。

【问题讨论】:

  • 如果性能是您的最终目标,我会推荐fgetc() 而不是scanf()
  • 那么,究竟是什么不起作用?你的代码做了什么,你期望它做什么?
  • 它本质上是一个翻译器。它遵循一个特定的映射,从文件中获取输入,并根据映射将输出写入文件。这就是它所做的,也是我期望它做的,只通过 linux 终端。

标签: c file io file-handling


【解决方案1】:

将您的文件编译为 foo,您可以执行以下操作:

./foo < input > output

这将读取输入并将您使用 printf 编写的所有内容写入输出。

【讨论】:

  • 嘿,非常感谢!这就像一个魅力。我现在可以松一口气了,我对输入/输出的事情感到非常紧张,因为事件快到了。非常感谢。
猜你喜欢
  • 2014-03-06
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 2015-12-05
相关资源
最近更新 更多