[英]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.