我試圖讀取文本文件和非打印ascii字符我想打印出「^」+「G」作爲BELL字符的示例。很像unix的cat -v命令。問題發生在我應該存儲字符的for循環中,直到我點擊一個換行符然後將它們打印出來。 for循環爲ctrl + G和「t」「e」「s」「t」打印「G」進行測試。C for循環迭代與putc不同的數組
int readFile(FILE* inputFile) {
char input[5];
char *arrayEnd = &input[5]+1;
int anyChanges = 1;
int iochar = 0;
int i = 0;
//get index of new line
//substring of position until new line
//print substring position to end.
int printedColumns = 0;
//credit Foster Chapter 2
while ((iochar = getc(inputFile)) != EOF)
{ //Returns 1 if no changes made, return 0 if any changes have been made.
//printf("character --> %c\n",iochar);
if(iochar != '\n') {
//This if statement checks for normal ascii characters.
//If the output is less than 72 it prints it and increments printedColumns.
if ((' ' <= iochar) && (iochar <= 126)) {
if(*(input + i) == *arrayEnd)
{
i = 0;
}
*(input +i) = iochar;
//printf("input array ---> %c\n",input[i]);
//printf("i:%d\n",i);
//printf("iochar:%d\n",iochar);
//putc(*(input+i), stdout);
i++;
}
//This if statement checks for the non-printing characters.
//New line is not included because it is a special case that is accounted for below
if (iochar <= 31) {
if (*(input + i) == *arrayEnd)
{
i = 0;
}
*(input + i) =94;
putc(*(input+i), stdout);
i++;
if(*(input+i)== *arrayEnd)
{
i = 0;
}
*(input + i) = iochar + 64;
putc(*(input+i), stdout);
printf("\n");
i++;
}
int b = 0;
for (b = 0;b<6;b++){
putc(*(input+b),stdout);
}
}//end if != '\n'
}//end while
return anyChanges;
}//end function
man ctype,esp isprint –
請勿使用數字;使用像'^'這樣的字符常量而不是94.爲什麼你在閱讀時不直接輸出每個字符的翻譯?您使用的微小緩衝區(5字符)的好處是什麼? –
我想最終打印最後72列,所以我將緩衝區設置得更小以便測試。 – user2946437