[英]Showing output in a certain way
我正在尝试打印出文本文件中所有字符的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
假设输入文件中的第一个单词是“ random”。 谢谢
是的,正如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)和我正在讨论自原始海报发表评论以来,如果缓冲区被填满会发生什么情况:
我假设输入文件最多包含1000个字符。
除非有一定数量的字符后出现换行符,否则输出将很长。
我们可以有一个最大缓冲区为1000的数组,但是通过仅每x
个字符处理一次文件,我们也可以节省更多的内存。 在这里,每隔NEW_TERMINAL_LINE
个字符后,我们可以创建一个新行并打印出当前缓冲区。 最后,我们可以清空缓冲区,然后继续处理字符。 实际上,使用这种方法, 缓冲区仅受计算机上可用内存的限制 。
假设我们希望将一行中的最大字符数(以及相应的ASCII值)显示为10。文本文件包含以下字符串: random random 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
。
我们最终将ASCII等效项存储在名为char_equivalents
的数组中。 对于遇到的每个字符,我们将其存储在数组中,然后将其打印出来,后跟一个制表符。
缓冲区数组填满后,我们向下移动一行,遍历数组,并使用以下格式打印出ASCII值:
由于char
本质上是伪装成ASCII的int
,因此我们只是将char
类型转换为int
。 这将打印出相应的ASCII值。 然后,我们添加一个标签以确保所有内容都与上面的行对齐。 有了这些信息,就不难遍历数组并打印出相应的信息。
最后,我们重置了缓冲区,并为清晰起见在NEW_TERMINAL_LINE
字符之后打印了空白的新行。
缓冲区将被填充并重置,直到我们在文件中打印了所有字符为止(重复前面的步骤)。
最后,有时缓冲区未满,因此我们从不为输出的最后一行打印出剩余的ASCII等价字符。 我们只需再次调用print_current_characters()
。
1我并不是真的认为这是格式化代码的最佳方法,但是与此同时,我确实想尊重原始帖子所要求的格式。
如果您处理单个单词而不是每个字符,它将变得更加方便。 这样,您可以先打印单词的每个字符,然后再打印单词的每个字符的ASCII值。
您可以尝试这样的操作。
int main(int argc, char* argv[]) {
FILE *fp;
int c;
char line[100]; /* Buffer to hold each line */
fp = fopen(argv[1], "r");
/* get each line, and print each word of the line */
while (fgets(line, 100, fp) != NULL) {
line[strlen(line) - 1] = '\0';
print_words(line);
}
fclose(fp);
return 0;
}
/* print_words: print each word from line */
void print_words(char *line)
{
char *word;
/* get each word, and print them with ascii values */
if ((word = strtok(line, " ")) != NULL)
ascii_print(word);
while ((word = strtok(NULL, " ")) != NULL)
ascii_print(word);
}
/* ascii_print: print each char and its ascii values for a word*/
void ascii_print(char *word)
{
int i, len;
len = strlen(word);
/* print the word */
for(i = 0; i < len; i++)
printf("%-4c", word[i]);
printf("\n");
/* print ascii values */
for(i = 0; i < len; i++)
printf("%-4d", word[i]);
printf("\n");
}
请注意,上述程序并非完全正确,在某些测试案例中可能会产生错误的结果。 但是,它显示出这样的想法:如果逐字处理而不是逐字处理,则更容易。
尽管代码很简单,但是如果您不熟悉它们,则可能必须查看fgets()
和strtok()
手册页。
简而言之, fgets()
从文件中获取line
,而strtok()
从一行中获取每个word
。 有关更多信息,您需要在Google中进行man 3 strtok
和man 3 fgets
的操作(如果使用类Unix机器,请在命令行中进行操作)。
您可以首先使用缓冲区在一行中记录输入,然后可以通过将缓冲区中的字符转换为下一行中的整数来显示输入的ASCII值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.