c - 以某种方式显示输出

标签 c

我正在尝试打印文本文件中所有字符的 ASCII 值。

int main(int argc, char* argv[]) {
        FILE *fp;
        int c;
        fp = fopen(argv[1], "r");
        while((c = fgetc(fp)) != EOF) {
                printf("%c %3d\n", c, (int)c);
        }
        fclose(fp);
        return 0;
}

是否可以显示如下所示的输出?

r     a     n     d     o     m
114   97   110   100   111   109

假设我的输入文件中的第一个单词是“随机”。谢谢

最佳答案

是的,如hexiecs说,很有可能。问如何会是一个更好的问题。我假设这就是你的意思。

以下是您可以如何执行此操作的示例:

#include <stdio.h>
#define NEW_TERMINAL_LINE 10

void print_current_characters(char char_equivalents[]);

int main(int argc, char const *argv[]) {
  char char_equivalents[NEW_TERMINAL_LINE] = {0};

  int c;
  int i = 0;
  int final_position; /* Store the final position that we will use later
                       * to get the remaining characters in the buffer */

  FILE *file;
  file = fopen("file.txt", "r");
  if (file) {
    while ((c = getc(file)) != EOF) {
        if (i > NEW_TERMINAL_LINE - 1) {
          /* Every time the buffer fills up, we need to go to a new line
           * in the output as well (defined by NEW_TERMINAL_LINE).
           * Then we need to clear the buffer and make a new line 
           * to start off printing again!
          */
          print_current_characters(char_equivalents);
          int j;
          for (j = 0; j < sizeof(char_equivalents)/sizeof(char_equivalents[0]); j++) {
            char_equivalents[j] = 0; /* Clear the filled up buffer */
            i = 0;
          }
          printf("\n\n");
        }
        /* Print the character itself (we will print the ASCII when the buffer fills up) */
        char_equivalents[i] = c;
        if(char_equivalents[i] == '\n') {
          printf("\\n\t");
        } else if (char_equivalents[i] == '\t') {
          printf("\\t\t");
        } else if (char_equivalents[i] == ' ') {
          printf("[space]\t");
        } else {
          printf("%c\t", c);
        }
        final_position = i;
        i++;
    }

    /* Print any remaining characters in the buffer */
    print_current_characters(char_equivalents);

    fclose(file);
  }
  printf("\n");
  return 0;
}
void print_current_characters(char char_equivalents[]) {
  printf("\n");
  int i;

  for(i = 0; i < NEW_TERMINAL_LINE; i++) {
    if (char_equivalents[i] == 0 || NULL) {
      break; /* Don't print if there is nothing but 0s in the buffer */
    }
    if(char_equivalents[i] == '\n') {
      printf("\\n\t\t");
    } else if (char_equivalents[i] == '\t') {
      printf("\\t\t");
    } else {
      printf("%d\t", (int)char_equivalents[i]); /* Print the ASCII character */
    }
  }
}

根据我之前的编辑,Jonathan Leffler我正在讨论如果缓冲区填满会发生什么,因为原始海报在评论中说:

I am assuming the input file will have a maximum 1000 characters.

输出将是一个非常长行,除非在一定数量的字符后面有换行符。

我们可以有一个最大缓冲区为 1000 的数组,但我们也可以通过每 x 个字符处理文件来节省更多内存。在这里,在每 NEW_TERMINAL_LINE 个字符之后,我们可以创建一个新行并打印出当前缓冲区。最后我们可以清空缓冲区,并继续处理字符。事实上,使用这种方法,缓冲区仅受计算机上的可用内存的限制

假设我们希望一行上的最大字符数(以及相应的 ASCII 值)显示为 10。文本文件包含字符串:random random random random random random [enter] (假设,字符串可能长得多,并且程序仍会整齐地显示它)。该程序的输出类似于1:

r   a   n   d   o   m   [space] r   a   n   
114 97  110 100 111 109 32     114  97  110 

d   o   m   [space] r   a   n   d   o   m   
100 111 109 32     114  97  110 100 111 109 

[space] r   a   n   d   o   m   [space] r   a   
32     114  97  110 100 111 109 32     114  97  

n   d   o   m   [space] r   a   n   d   o   
110 100 111 109 32     114  97  110 100 111 

m   [space] r   a   n   d   o   m   [space] r   
109 32     114  97  110 100 111 109 32     114  

a   n   d   o   m   \n  
97  110 100 111 109 \n

如果 NEW_TERMINAL_LINE 设置为 10

  1. 我们最终将 ASCII 等效项存储在名为 char_equivalents 的数组中。对于我们遇到的每个字符,我们将其存储在数组中,然后将其打印出来,后跟制表符。

  2. 一旦缓冲区数组已满,我们向下移动一行,循环遍历数组,并按格式打印出 ASCII 值:

    由于 char 本质上是伪装成 ASCII 的 int,因此我们只需将 char 类型转换为 int >。这将打印出相应的 ASCII 值。然后,我们添加一个选项卡以确保所有内容都与上面的行对齐。有了这些信息,循环遍历数组并打印出相应的信息就不难了。

  3. 我们最终重置了缓冲区,并打印在 NEW_TERMINAL_LINE 字符后看到的空白新行以确保清晰。

  4. 缓冲区被填充并重置,直到我们打印完文件中的所有字符(重复前面的步骤)。

  5. 最后,有时缓冲区未满,因此我们从未打印出输出中最后一行的剩余 ASCII 等效项。我们只需再次调用 print_current_characters() 即可。

<小时/>

1 我真的不认为这是格式化代码的最佳方式,但同时,我确实想遵循 original post 的格式。要求。

关于c - 以某种方式显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711239/

相关文章:

c++ - C++ 中的命名冲突 : How to access a struct member called "class"

c++ - 不使用 for、while 或 goto 的循环

c - 我使用 setjmp un longjmp 退出(状态)

c++ - 带主题的 C/C++ 编译器和编辑器

c - 按位乘以 5/8 观察溢出

c - 如何从C中的输入中删除垃圾字符

c - 我对内存管理和分页的想法是否正确?

c - C中的char string [LENGTH]和char * string [LEN]有什么区别

c - 从固定闪存位置访问 C 字符串

c - 将字符串写入文件